我是编码的新手,正在尝试使用pycharm和discord.py在python-3.x中创建一个discord机器人。我希望它仅在nsfw通道中发送消息,如果该通道未标记为nsfw,则发送诸如“请在nsfw通道中使用此命令”之类的错误消息,问题是我不知道从哪里开始!我在堆栈溢出时找不到任何类似的问题。任何帮助将不胜感激。
答案 0 :(得分:0)
如果要在NSFW通道发出命令时发送消息,则可以在on_message()
函数中执行此操作:
if (message.channel.nsfw): # if message came from a NSFW channel, do this
await message.channel.send("You are in a NSFW channel")
else: # otherwise, do this
await message.channel.send("You are not in a NSFW channel. Please use this command in a NSFW channel")
完整测试的代码:
from discord.ext.commands import Bot
bot = Bot(command_prefix="!")
@bot.event
async def on_message(message):
if message.author == bot.user: #if message sent by bot, do nothing
return
else:
if (message.channel.nsfw): # if message came from a NSFW channel, do this
await message.channel.send("You are in a NSFW channel")
else: # otherwise, do this
await message.channel.send("You are not in a NSFW channel. Please use this command in a NSFW channel")
bot.run("TOKEN")