Im making a dumb discord bot, but it crashes everytime when he's muted or in timeout. How do i add a send messages permission check?
import discord
import random
TOKEN = "blahblahblah"
client = discord.Client()
@client.event
async def on_ready():
print("{0.user} is now online!".format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if 'blah' in message.content or client.user.mentioned_in(message):
print(message.author, ":", message.content)
if random.randint(1,2) == 2:
await message.channel.send('blah?')
else:
await message.channel.send('blah!')
client.run(TOKEN)
When he's in timeout and trying to send a message, python gives me a 403 forbidden error.
All of the solutions i found in the internet were confusing, most of them didn't work
CodePudding user response:
You can catch exceptions like this:
try:
await message.channel.send("blah")
except discord.Forbidden:
print("Bot is missing permissions")
I highly recommend using commands.Bot()
instead of discord.Client()
as it comes with several benefits such as error handling, support for the commands extension, and checks among other things.
https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html