[英]Discord Bot not responding to commands... (Using Pycord)
我试图制作一个 discord 机器人,我希望它的第一个命令非常简单,我只希望它在有人键入“.ping”但我尝试运行命令时响应“pong”。 什么也没有发生..我也尝试过使用其他命令,但它们都会导致错误,我是使用 Python 的初学者,所以好吗? 有人可以告诉我我的代码有什么问题,以及如何修复它吗?
简而言之,当我键入我为其执行的命令时,我的机器人没有响应。
import discord
import os
from discord.ext import commands
from discord.ext.commands import Bot
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
@client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
@bot.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
而不是@bot.command
你应该使用@client.command
。
也不需要from discord.ext.commands import Bot
import
所以你的新代码是
import discord
import os
from discord.ext import commands
case_insensitive=True
client = discord.Client()
bot = commands.Bot(
command_prefix="!",
intents=discord.Intents(members=True, messages=True, guilds=True)
)
@client.event
async def on_ready():
print('logged in as {0.user}!'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
@client.command(name="ping", description= "Find out!")
async def ping(ctx):
await ctx.send("Pong!")
client.run(os.getenv('TOKEN'))
你可以在on_message
代码块里面做
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Pong!')
client.run('your token here')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.