Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check discord bot's permissions so it wont crash

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading