【问题标题】:discord bot not responding to commands不和谐机器人不响应命令
【发布时间】:2021-06-17 12:53:49
【问题描述】:

我试图制作一个不和谐的机器人,并试图为它创建一个命令。但机器人不响应命令。代码如下:

import discord
from discord.ext import commands
import logging

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='err.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

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

token = 'BOT TOKEN'
GUILD = 'My test server'
prefix = bot.command_prefix

@bot.event
async def on_ready():
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} is connected to the following server:\n'
        f'{guild.name}(id: {guild.id})'
    )
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="minecraft"))


#here is the command where the bot is not responding to
@bot.command(name='say')
async def say(ctx, arg):
    await ctx.send(arg)

bot.run(token)

任何帮助???

【问题讨论】:

  • 一切正常,你真的在​​运行文件吗?
  • 是的,输出如下:""the bot name"" is connected to the following server: My test server(id: 818450344492400710)
  • 您确定机器人有权在该频道中发送消息吗?尝试添加一个on_message 函数来打印和发送用户所说的任何内容
  • 它有管理员权限
  • 下面的答案有效,谢谢 Aditya Tomar!

标签: python-3.x command discord.py


【解决方案1】:

您需要一个on_message 事件。

import discord
from discord.ext import commands
import logging

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='err.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

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

token = 'BOT TOKEN'
GUILD = 'My test server'
prefix = bot.command_prefix

@bot.event
async def on_ready():
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} is connected to the following server:\n'
        f'{guild.name}(id: {guild.id})'
    )
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="minecraft"))

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    await bot.process_commands(message)

#here is the command where the bot is not responding to
@bot.command() # you don't need name to be say, since it's already say in the command definition
async def say(ctx, arg):
    await ctx.send(arg)

bot.run(token)

【讨论】:

  • 为什么要这样做? discord.py 已经实现了这个 here
猜你喜欢
  • 2021-08-26
  • 2018-07-21
  • 2018-11-10
  • 2021-11-21
  • 2021-10-21
  • 2022-01-15
  • 2017-05-31
  • 2019-04-13
  • 1970-01-01
相关资源
最近更新 更多