如何添加允许启动和停止discord bot而不完全结束其进程的命令(python)

2024-06-16 08:57:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个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'))

Tags: inreclientsendmessageifonchannel
2条回答

一种简单的方法是使用一个(全局)布尔变量enabled,当接收命令!start时,该变量被切换到True,在!stop命令上被切换到False。然后,当检查除!start!stop以外的其他命令时,首先检查enabled的值,并且仅当enabledTrue时才执行这些命令。在接收这两个命令之一时发送消息也很简单

这可能看起来像这样:

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级别使用bot.check装饰器处理检查

然后,您将实现一个具有适当权限的命令,该命令允许您启动/停止bot活动。例如,我只允许自己运行控制命令

以下是我设计创意的好方法:

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

相关问题 更多 >