简体   繁体   中英

how to make a discord.py bot not accepts commands from dms

How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.

If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:

channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id

Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode . After than you could just right click on the server or channel and copy the id.

To get a server's id you need to add this piece of code as a command:

@client.command(pass_context=True)
async def getguild(ctx):
    id = ctx.message.guild.id # the guild is the server
    # do something with the id (print it out)

After you get the server id, you can delete the method.

And to check if a message is sent by a person or a bot, you could do this in the on_message method:

def on_message(self, message):
    if (message.author.bot):
        # is a bot
        pass

So just to make the bot not respond to DMs, add this code after each command:

if message.guild:
  # Message comes from a server.
else:
  # Message comes from a DM.

This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.

I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,

@client.event
async def on_message(message):
if message.author.id == <#your user id>:
    await message.channel.send('message detected')
    ...#your code

You Can Use Simplest And Best Way

@bot.command()
async def check(ctx):
    if not isinstance(ctx.channel, discord.channel.DMChannel):
       Your Work...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM