Discord bot discord.py命令无法使用。

问题描述 投票:1回答:1

我是一个相当新的制作discord机器人的人,我想做一个基本的测试命令,你说(前缀)测试abc,机器人也说abc。我没有得到任何错误,但是当我输入r!test abc时,什么都没有发生。我在终端也没有得到任何结果。

这是我的代码。

import discord

from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()


GUILD = os.getenv('DISCORD_GUILD') # Same thing but gets the server name
client = discord.Client()
bot = commands.Bot(command_prefix='r!')
TOKEN = 'TOKENGOESHERE'
on = "I'm up and running!"
print("Booting up...")
channel = client.get_channel(703869904264101969)

@client.event # idk what this means but you have to do it

async def on_ready(): # when the bot is ready

    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(f'{client.user} has connected to Discord! They have connected to the following server: ' #client.user is just the bot name
    f'{guild.name}(id: {guild.id})' # guild.name is just the server name


    )

    channel2 = client.get_channel(channelidishere)
    await channel2.send(on)
#everything works up until I get to here, when I run the command, nothing happens, not even some output in the terminal.

@commands.command()
async def test(ctx):
    await ctx.send("test")



client.run(TOKEN)

谢谢大家!

python command discord.py
1个回答
1
投票

关键问题是你没有正确定义你的装饰符。因为你使用的是命令,你只需要 bot = commands.Bot(command_prefix='r!') 声明: 该 client = discord.Client() 不需要。因为它是 bot = ...,所有的装饰者都需要从 @bot. 此外,你不会使用客户端,你会使用机器人,如 channel2 = bot.get_channel(channelidishere).

去掉了公会循环,换成了...。discord.utils.get 得到公会- guild = discord.utils.get(bot.guilds, name=GUILD). 这就需要另一个导入- import discord

试试这个

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')  # Same thing but gets the server name
bot = commands.Bot(command_prefix='r!')
on = "I'm up and running!"
print("Booting up...")


@bot.event  # idk what this means but you have to do it
async def on_ready():  # when the bot is ready
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} has connected to Discord! They have connected to the following server: '  # client.user is just the bot name
        f'{guild.name}(id: {guild.id})')  # guild.name is just the server name
    channel2 = bot.get_channel(channelidishere)
    await channel2.send(on)


@bot.command()
async def test(ctx):
    await ctx.send("test")


bot.run(TOKEN)
© www.soinside.com 2019 - 2024. All rights reserved.