Команды Discord bot discord.py не работают - PullRequest
1 голос
/ 02 мая 2020

Я довольно новичок в создании диссон-ботов, и я пытаюсь создать базовую c тестовую команду, где вы говорите (префикс) test ab c, а бот тоже говорит ab c. Я не получаю ошибок, но когда я набираю r! Test ab c, ничего не происходит. Я тоже ничего не получаю в терминале.

Вот мой код.

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)

Спасибо всем!

1 Ответ

1 голос
/ 02 мая 2020

Ключевой вопрос заключается в том, что вы неправильно определяете свои декораторы. Поскольку вы используете команды, вам нужен только оператор bot = commands.Bot(command_prefix='r!'). client = discord.Client() не требуется. Поскольку это bot = ..., все ваши декораторы должны начинаться с @bot. Кроме того, вы не будете использовать клиента, вы будете использовать бота типа channel2 = bot.get_channel(channelidishere).

. Убрал гильдию l oop и заменил ее на 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)
...