繁体   English   中英

如何使 discord.py 机器人不接受来自 dms 的命令

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

如何让 discord.py 机器人不响应来自机器人 DM 的命令? 我只希望机器人在特定服务器上的特定频道上响应消息。

如果您只想回复特定频道上的消息并且您知道该频道的名称,则可以执行以下操作:

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

然后,您将检查 id 是否与您希望它所在的一个频道匹配。要获取频道或服务器的 id,您需要启用discord developer mode 之后,您只需右键单击服务器或频道并复制 ID。

要获取服务器的 id,您需要将这段代码添加为命令:

@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)

获得服务器 ID 后,您可以删除该方法。

要检查消息是由人还是机器人发送的,您可以在 on_message 方法中执行此操作:

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

因此,为了使机器人不响应 DM,请在每个命令后添加此代码:

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

这样可以更好地将 DM 与服务器消息分开。 您刚才必须移动“await message.channel.send”function。

我假设您正在要求一个只听您的命令的机器人。 好吧,在这种情况下,您可以创建一个检查以查看消息是否由您发送。 可以使用,

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

你可以使用最简单和最好的方法

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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