我正在尝试创建一个discord bot,bot的目的是在检测到特定单词时输出特定字符串
我已经想出了如何做到这一点,它工作得很好,但我真正努力的是如何给机器人启动和停止命令,看起来像这样:!开始停止我还想在每个命令被触发时输出一条消息,以便让用户知道它已经工作了
感谢您的帮助,并提前向您表示感谢
import os
import discord
from keep_on import keep_on
bot_token = os.environ['TOKEN']
client = discord.Client()
@client.event
async def on_ready():
print('{0.user} is online'.format(client))
@client.event
async def on_message(message):
sentWord = message.content
CSentWord = sentWord.upper()
if message.author == client.user:
return
if "SORRY" in CSentWord:
await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
await message.channel.send(file=discord.File('Joey Is Sorry.png'))
if "S_O_R_R_Y" in CSentWord:
await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
await message.channel.send(file=discord.File('Joey Is Sorry.png'))
if "S.O.R.R.Y" in CSentWord:
await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
await message.channel.send(file=discord.File('Joey Is Sorry.png'))
if "S|O|R|R|Y" in CSentWord:
await message.channel.send("'We're Very Sorry' - Joey Tribbiani")
await message.channel.send(file=discord.File('Joey Is Sorry.png'))
keep_on()
client.run(os.getenv('TOKEN'))
一种简单的方法是使用一个(全局)布尔变量
enabled
,当接收命令!start
时,该变量被切换到True
,在!stop
命令上被切换到False
。然后,当检查除!start
和!stop
以外的其他命令时,首先检查enabled
的值,并且仅当enabled
为True
时才执行这些命令。在接收这两个命令之一时发送消息也很简单这可能看起来像这样:
虽然使用
global
是一种已知的错误做法,但这将作为第一种方法您必须添加某种全局检查,并且您有许多选项来执行此操作。例如,您可以在
on_command
事件中处理检查,或者甚至在bot级别使用bot.check
装饰器处理检查然后,您将实现一个具有适当权限的命令,该命令允许您启动/停止bot活动。例如,我只允许自己运行控制命令
以下是我设计创意的好方法:
相关问题 更多 >
编程相关推荐