你用什么命令来运行机器人

时间:2021-01-22 19:47:40

标签: python discord discord.py

你是怎么做到的?

这是我得到的:

import os

import discord
from discord.ext import commands
from discord.ext.commands import Context
from dotenv import load_dotenv, find_dotenv

load_dotenv(find_dotenv())
BOT_TOKEN = os.getenv("token")

client = commands.Bot(command_prefix='prefix')


@client.command()
async def load(ctx: Context, extension):
    client.load_extension(f'cogs.{extension}')
    await ctx.send("Loaded Cog")


@client.command()
async def unload(ctx: Context, extension):
    client.unload_extension(f'cogs.{extension}')
    await ctx.send("Unloaded Cog")


@client.command()
async def reload(ctx: Context, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')
    await ctx.send("Reloaded Cog")

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[0:-3]}')

client.run(token)

这样对吗?还是错了?

我很喜欢编码和这类东西,所以如果你能帮助我做得更好,我将不胜感激:-)

1 个答案:

答案 0 :(得分:1)

您从不定义令牌变量,而是在 bot.run(token) 中使用它。我建议将您定义的 BOT_TOKEN 变量名称更改为 token 或将 token 中的 bot.run(token) var 替换为 bot.run(BOT_TOKEN)