[英]discord.py ctx commands do not return anything
我正在尝试在 discord 中使用机器人命令,这样我就可以开始使用机器人前缀了。 我尝试使用 ctx,但是从我编写的代码中,当我使用 .ping 时,它什么也不返回。 有人知道为什么吗?
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix= ".")
@client.event
async def on_ready():
print("Logged in") #login message
@bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
@client.event
async def on_message(message):
if message.author == client.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
client.run('#bot token here')
您的代码中有很多错误:
discord.Client
或commands.Bot
不是两者message.author
是None
,方法如下:intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(..., intents=intents)
还要确保在开发人员门户中启用特权意图
client
on_message
和on_ready
事件的装饰器,并在on_message
末尾添加process_commands
:@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
这是您的固定代码:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix= ".", intents=intents)
@bot.event
async def on_ready():
print("Logged in")
@bot.command()
async def ping(ctx):
await ctx.send("test")
@bot.event
async def on_message(message):
if message.author == bot.user:
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
我发现您的代码中有一些错误。
discord.Client()
和commands.Bot()
然后编写这段代码:
intents = discord.Intents.all()
bot = commands.Bot(commands_prefix=“.”, intents=intents)
确保您没有在此处使用任何client
。
还有更多的东西需要修复。 由于现在我们使用的是机器人,所以将所有说客户端的内容更改为机器人。 您还必须将await bot.process_commands(message)
添加到on_message
function 的末尾。 ctx.channel.send
更改为ctx.send
@bot.event
async def on_ready():
print("Logged in") #login message
@bot.command(pass_context = True) #trying to get "test" to return from .ping
async def ping(ctx):
await ctx.channel.send("test")
@bot.event
async def on_message(message):
if message.author == bot.user: #testing the bot actually responds to something
return
if message.content.startswith("Hello"):
await message.channel.send("Hi")
await bot.process_commands(message)
bot.run('#bot token here')
只需使用简单的ctx.send
,这对于命令很常见,
@bot.command()
async def ping(ctx):
await ctx.send("test")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.