[英]Discord.py how to make command Channel Specific
So I've been trying to make certain commands channel specific.所以我一直在尝试使某些命令通道特定。 I can get the entire code to be that way but not certain commands.
我可以获得整个代码,但不是某些命令。
async def on_message(message):
if message.author == bot.user:
return
if message.author.bot: return
cmdChannel = bot.get_channel(780617002267967508)
if message.channel.id == cmdChannel.id:
if ' ' in message.content:
channel = message.channel
await channel.send("Admin Commands <@287750380299747338>")
return
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.author.bot: return
cmdChannel = bot.get_channel(780599527664255003)
if message.channel.id == cmdChannel.id:
if ' ' in message.content:
channel = message.channel
await channel.send("Admin Chat <@287750380299747338>")
return
In my code, When I type a message with a space in my Admin Commands channel, I do not get a response from the bot, but it works perfectly fine in my Admin Chat channel.在我的代码中,当我在 Admin Commands 频道中键入带有空格的消息时,我没有收到机器人的响应,但它在我的 Admin Chat 频道中工作得很好。 I want the bot to auto respond to messages with spaces in them for both channels, but have different responses based on the channel.
我希望机器人自动响应两个频道中包含空格的消息,但根据频道有不同的响应。 For example:
例如:
I would type "Test Message" in admin commands, and it would return "Admin Commands @username"我会在管理命令中输入“测试消息”,它会返回“管理命令@用户名”
I would type "Test Message" in admin chat, and it would return "Admin Chat @username"我会在管理员聊天中输入“测试消息”,它会返回“管理员聊天@用户名”
What am I doing wrong?我究竟做错了什么? How can this be fixed?
这怎么能解决?
I notice a few things in your code:我注意到您的代码中有几件事:
First) You cannot have multiple on_message
events, then the code won't work.首先)你不能有多个
on_message
事件,那么代码将不起作用。
Second) You check multiple times to see if the message is coming from a bot.第二)您检查多次以查看消息是否来自机器人。 Why?
为什么? Once is enough.
一次就够了。
Third) You don't need to query message.channel.id
at all if you define the channel beforehand.第三)如果您事先定义了频道,则根本不需要查询
message.channel.id
。 Then simply message.channel == DefinedChannel
is enough.然后只需
message.channel == DefinedChannel
就足够了。
Have a look at the following code:看看下面的代码:
@bot.event
async def on_message(message):
if message.author == bot.user: # Check if the message comes from the bot
return # Do nothing
if ' ' in message.content: # Check if there is a space in the message
admin_channel = client.get_channel(ChannelID) # Define our channel
admin_commands = client.get_channel(ChannelID) # Define our second channel
if message.channel == admin_channel: # Check if the message is in admin_channel
await admin_channel.send(f"Admin Chat {message.author.mention}") # Mention the author
elif message.channel == admin_commands: # Check if the message is in admin_commands
await admin_commands.send(f"Admin Commands {message.author.mention}")
else: # If the message is not in any of the two channels.
return # Do nothing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.