0
votes

I recently had the idea of DMing my bot commands. For example, a command which would unban me from every server that the bot is on.

Unfortunately I don't have any starting point for the command because I'm not even sure DMing commands is possible.

Keywords like discord.py, command or DM are so common in google that finding any good info on the topic is very hard.

I'm looking for a way for the bot to receive DMs as commands and only accept them from me (my ID is stored in the variable ownerID if anyone wants to share any code).

While I'm mostly looking for the above, some code for the DM unban command would also be very helpful.

EDIT: I was asked to show some example code from my bot. Here is the code for the command number which generates a random number and sends it in a message. I hope this gives you an idea of how my bot is made:

@BSL.command(pass_context = True)
async def number(ctx, minInt, maxInt):
    if ctx.message.author.server_permissions.send_messages or ctx.message.author.id == ownerID:
        maxInt = int(maxInt)
        minInt = int(minInt)
        randomInt = random.randint(minInt, maxInt)
        randomInt = str(randomInt)
        await BSL.send_message(ctx.message.channel, 'Your random number is: ' + randomInt)
    else:
        await BSL.send_message(ctx.message.channel, 'Sorry, you do not have the permissions to do that @{}!'.format(ctx.message.author))
1
You haven't posted any of your current code. How do you expect to get help. Depending on how you'd written your bot accepting dms as commands would be different. - Wright
That's true. Sorry about that. I will add some example code from my bot as soon as I can. - user9123

1 Answers

2
votes

You can send commands in private messages. Something like

@BSL.command(pass_context=True)
async def unban(ctx):
    if ctx.message.channel.is_private and ctx.message.author.id == ownerID:
        owner = await BSL.get_user_info(ownerID)
        await BSL.say('Ok {}, unbanning you.'.format(owner.name))
        for server in BSL.servers:
            try:
                await BSL.unban(server, owner) # I think having the same name should be fine.  If you see weird errors this may be why.
                await BSL.say('Unbanned from {}'.format(server.name))
            except discord.HTTPException as e:
                await BSL.say('Unbanning failed in {} because\n{}'.format(server.name, e.text))
            except discord.Forbidden:
                await BSL.say('Forbidden to unban in {}'.format(server.name))
    else:
        if ctx.message.author.id != ownerID:
            await BSL.say('You are not my owner')
        if not ctx.message.channel.is_private:
            await BSL.say('This is a public channel')

should work. I'm not sure what happens if you try to unban a user who is not banned, that may be what raises HTTPException