【问题标题】:Discord bot command works some timesDiscord bot 命令有时有效
【发布时间】:2021-01-23 01:26:53
【问题描述】:

我对@9​​87654322@ 很陌生,我正在构建一个可以根据命令关闭的机器人(但这不会关闭/使机器人脱机,它只是禁用它的功能)。所以我做了这个shutdown() 命令,有时可以工作,而有时它对我来说不是未知的原因。有些打印也因为某种原因不能工作,我会在代码中标记它们:

import discord
import os
from dotenv import load_dotenv
from discord.ext import commands
import random

load_dotenv()

token = 'bot token'

bot = commands.Bot(command_prefix = '$')

@bot.event
async def on_ready():
    print('-----------------------------------')
    print('we have logged in as {0.user}'.format(bot))
    print('-----------------------------------')

@bot.command()
async def ping(ctx):
    await ctx.channel.send("pong")

@bot.command()
async def say(ctx, *args):
    response = ""

    for arg in args:
        response = response + " " + arg

    await ctx.channel.send(response)

@bot.command()
async def shutdown(ctx):#this command doesn't work properly
    await bot.close()
    print("We have logged off as {0.user}".format(bot))#this print doesn't work most of the time

@bot.event
async def on_message(message):
    if message.content == "opinion":
        await message.channel.send("not an opinion")

bot.run(token)

感谢转发!任何帮助表示赞赏!

【问题讨论】:

  • 你可能想要bot.logout() 而不是.close()
  • @effprime Bot.logout 只是Bot.close 的别名,看看docs

标签: python discord discord.py dotenv


【解决方案1】:

你的机器人没有处理命令(其他命令也应该不起作用),你应该在on_message事件的结束添加bot.process_commands

@bot.event
async def on_message(message):
    # ...

    await bot.process_commands(message)

参考:

-Bot.process_commands

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2020-09-29
    • 2017-11-21
    • 2020-05-28
    • 2020-10-25
    相关资源
    最近更新 更多