The bot runs without any error, but it just won't listen to any commands you give it such as : '!join', '!leave', '!play', etc. Not even events, nothing. It worked yesterday, but today... I don't know...
Here is the code :
import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
from better_profanity import profanity
import random
import os
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
import apikeys as ak
import rapidAPI_Joker as rj
@bot.event
async def on_ready():
print("--------------------------------")
print("The bot is now ready for use!")
print("--------------------------------")
@bot.event
async def on_member_join(member):
channel = bot.get_channel(1161221526150987807)
await channel.send(f"Hello {member.display_name}! Welcome to my discord server!")
await channel.send(rj.setup)
await channel.send(rj.punchline)
@bot.event
async def on_member_remove(member):
channel = bot.get_channel(1161221526150987807)
await channel.send(f"Goodbye {member.display_name}!")
@bot.event
async def on_message(message):
greeting = ['Are you daft?', 'With that mouth you kiss your mother?', 'Imagine you told that to your girlfriend!']
if profanity.contains_profanity(message.content):
await message.delete()
await message.channel.send(random.choice(greeting))
@bot.command(pass_context=True)
async def join(ctx):
if ctx.author.voice:
channel = ctx.message.author.voice.channel
voice = await channel.connect()
source = FFmpegPCMAudio('BO2REE.mp3')
player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
print('Join')
else:
await ctx.send('You are not in a voice channel!')
@bot.command(pass_context=True)
async def leave(ctx):
if ctx.voice_client:
await ctx.guild.voice_client.disconnect()
await ctx.send('I left the voice channel!')
else:
await ctx.send('I\'m not in a voice channel!')
@bot.command(pass_context=True)
async def pause(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.pause()
else:
await ctx.send('There\'s no audio playing!')
@bot.command(pass_context=True)
async def resume(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
if voice.is_paused():
voice.resume()
else:
await ctx.send('No audio paused!')
@bot.command(pass_context=True)
async def stop(ctx):
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
voice.stop()
queues = {}
def check_queue(ctx, id):
if queues.get(id):
if queues[id] != []:
voice = ctx.guild.voice_client
source = queues[id].pop(0)
player = voice.play(source)
@bot.command(pass_context=True)
async def play(ctx, audio):
voice = ctx.guild.voice_client
audio_file = audio + '.mp3'
if not os.path.isfile(audio_file):
await ctx.send('The specified audio file does not exist.')
return
source = FFmpegPCMAudio(audio_file)
player = voice.play(source, after=lambda x=None: check_queue(ctx, ctx.message.guild.id))
await ctx.send('Now playing: ' + audio)
@bot.command(pass_context=True)
async def queue(ctx, audio):
voice = ctx.guild.voice_client
audio_file = audio + '.mp3'
if not os.path.isfile(audio_file):
await ctx.send('The specified audio file does not exist.')
return
source = FFmpegPCMAudio(audio_file)
guild_id = ctx.message.guild.id
if guild_id in queues:
queues[guild_id].append(source)
else:
queues[guild_id] = [source]
await ctx.send(f'Added {audio_file} to queue')
bot.run(ak.bot_token)
Reset the token from the Discord Developer Portal and pasted it back into the code.
Switched on/off all Privileged Gateway Intents to see if I can get it to work.
Created another bot in the Discord Developer Portal and did the same setup as the initial one.
Reinstalled discord.py package
Reinstalled each package (package by package)
I RAN THE CODE YESTERDAY AND IT WORKED WITHOUT ANY PROBLEMS WITH MODULES (FILES) CREATED BY ME!
Note : By modules created by me, I mean files like : 'apikeys.py' and 'rapidAPI_Joker.py'.
I'll answer my own question, because people have their own problems to solve.
ADDED THIS LINE OF CODE :
"await bot.process_commands(message)"
BEFORE :
@bot.event
async def on_message(message : discord.Message):
greeting = ['Are you daft?' , 'With that mouth you kiss your mother?' , 'Imagine you told that to your girlfriend!']
if profanity.contains_profanity(message.content):
await message.delete()
await message.channel.send(r.choice(greeting))
AFTER :
@bot.event
async def on_message(message : discord.Message):
greeting = ['Are you daft?' , 'With that mouth you kiss your mother?' , 'Imagine you told that to your girlfriend!']
if profanity.contains_profanity(message.content):
await message.delete()
await message.channel.send(r.choice(greeting))
await bot.process_commands(message)
Now, the bot listens to any command you give it or even events.
REASON : The line of code added to the program allows the Discord bot to continue processing and handling other commands even if a message is deleted due to containing profanity. It keeps the bot's overall functionality intact and responsive to user interactions.