Python 3.x discord bot不响应命令
我试图创建一个不和谐的机器人,我试图为它创建一个命令。但是机器人不响应命令。代码如下:Python 3.x discord bot不响应命令,python-3.x,command,discord.py,Python 3.x,Command,Discord.py,我试图创建一个不和谐的机器人,我试图为它创建一个命令。但是机器人不响应命令。代码如下: 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=
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)
有什么帮助吗?您需要一个on_消息事件
一切正常,您是否正在运行该文件?是的,以下是输出:bot名称已连接到以下服务器:My test serverid:818450344492400710您确定bot具有在该通道中发送消息的权限吗?尝试添加一个on_消息功能,打印并发送用户所说的任何内容。该功能具有管理员权限。下面的答案有效,谢谢!为什么要这样做?discord.py已经实现了这一点
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)