繁体   English   中英

discord.py ctx 命令不返回任何内容

[英]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')

您的代码中有很多错误:

  1. 使用discord.Clientcommands.Bot不是两者
  2. 您没有启用意图,所以message.authorNone ,方法如下:
intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(..., intents=intents)

还要确保在开发人员门户中启用特权意图

  1. 您正在运行客户端,而不是机器人,如果您希望命令运行运行机器人并摆脱client
  2. 更改on_messageon_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')

我发现您的代码中有一些错误。

  1. 不要同时使用discord.Client()commands.Bot()
  2. 您需要启用意图。 Go 到 discord 开发人员页面,然后到您的机器人并启用这两个意图。 快速链接到 Discord 上的开发人员页面

打开意图

然后编写这段代码:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM