如何添加允许我在不完全结束其进程的情况下启动和停止 discord 机器人的命令 (python)
How do I add commands that allow me to start and stop a discord bot without completely ending its process (python)
我正在尝试创建一个 discord 机器人,该机器人的目的是在检测到特定单词时输出特定字符串。
我已经想出了如何做到这一点并且它工作得很好,但我真正苦苦挣扎的是如何给机器人启动和停止命令,看起来像这样:!start, !stop。我还想在触发每个命令时输出一条消息,以便让用户知道它已经起作用。
感谢任何帮助,并提前致谢。
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
,然后切换到 False
在 !stop
命令上。然后在检查除!start
和!stop
以外的其他命令时,首先检查enabled
的值,只有当enabled
为True
时才执行命令。在收到这两个命令之一时发送消息也很简单。
这看起来像这样:
enabled = False
# Method called when the bot receives a message
async def on_message(message):
global enabled
if message.content == "!start":
enabled = True
await message.channel.send("Bot is on.")
elif message.content == "!stop":
enabled = False
await message.channel.send("Bot is off.")
elif enabled:
# Do whatever is done when the bot receives a message normally
# ...
虽然使用 global
是一种已知的不良做法,但这可以作为第一种方法。
您必须添加某种全局检查,并且您有很多选择可以做到这一点。例如,您可以在 on_command
事件中处理检查,甚至可以在机器人级别使用 bot.check
装饰器。
然后您将执行具有适当权限的命令,允许您 start/stop 机器人 activity。例如,我只允许我自己 运行 控制命令。
这是我设计想法的好方法:
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
is_active = True
@bot.check
async def isactive(ctx):
if not is_active:
await ctx.send(embed=a_nice_embed_showing_inactive_status)
return
return True
# the following essentially creates your own decorator to wrap control commands like start and stop.
def isme():
def control_command(ctx):
return ctx.author.id == my_id
return commands.check(control_command)
@bot.command()
@isme()
async def stop(ctx):
if not is_active:
await ctx.send("The bot is already active!")
else:
is_active = False
await ctx.send("The bot is active now.")
@bot.command()
@isme
async def start(ctx):
# exact opposite of stop
@bot.command()
async def random_command(ctx):
# this will fail if not is_active
bot.run("token")
我正在尝试创建一个 discord 机器人,该机器人的目的是在检测到特定单词时输出特定字符串。
我已经想出了如何做到这一点并且它工作得很好,但我真正苦苦挣扎的是如何给机器人启动和停止命令,看起来像这样:!start, !stop。我还想在触发每个命令时输出一条消息,以便让用户知道它已经起作用。
感谢任何帮助,并提前致谢。
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
,然后切换到 False
在 !stop
命令上。然后在检查除!start
和!stop
以外的其他命令时,首先检查enabled
的值,只有当enabled
为True
时才执行命令。在收到这两个命令之一时发送消息也很简单。
这看起来像这样:
enabled = False
# Method called when the bot receives a message
async def on_message(message):
global enabled
if message.content == "!start":
enabled = True
await message.channel.send("Bot is on.")
elif message.content == "!stop":
enabled = False
await message.channel.send("Bot is off.")
elif enabled:
# Do whatever is done when the bot receives a message normally
# ...
虽然使用 global
是一种已知的不良做法,但这可以作为第一种方法。
您必须添加某种全局检查,并且您有很多选择可以做到这一点。例如,您可以在 on_command
事件中处理检查,甚至可以在机器人级别使用 bot.check
装饰器。
然后您将执行具有适当权限的命令,允许您 start/stop 机器人 activity。例如,我只允许我自己 运行 控制命令。
这是我设计想法的好方法:
from discord.ext import commands
bot = commands.Bot(command_prefix = "!")
is_active = True
@bot.check
async def isactive(ctx):
if not is_active:
await ctx.send(embed=a_nice_embed_showing_inactive_status)
return
return True
# the following essentially creates your own decorator to wrap control commands like start and stop.
def isme():
def control_command(ctx):
return ctx.author.id == my_id
return commands.check(control_command)
@bot.command()
@isme()
async def stop(ctx):
if not is_active:
await ctx.send("The bot is already active!")
else:
is_active = False
await ctx.send("The bot is active now.")
@bot.command()
@isme
async def start(ctx):
# exact opposite of stop
@bot.command()
async def random_command(ctx):
# this will fail if not is_active
bot.run("token")