我在这里遇到了这段代码的问题...我从编程开始,这个不和谐机器人是我的第一个更大的项目,我想学习它
# bot.py
import os
import random
import discord
import time
import sys
import logging
from discord.ext import commands
from dotenv import load_dotenv
# importing your Discord Token
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
# create bot var and prefix
bot = commands.Bot(command_prefix='h!')
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding = 'utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
def restart_bot():
python = sys.executable()
os.execl(python, python, * sys.argv)
@bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
channel = discord.utils.get(bot.get_all_channels(), name='testl')
await channel.send("Ich bin wieder da")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CheckFailure):
await ctx.send('Du hast die benötigten Rechte nicht!')
@bot.command(name='69', help='Just nice!')
async def six_nine(ctx):
await ctx.send('nice!')
@bot.command(name='roll-dice', help='(würfel, augen) simuliert einen würfel')
async def roll_dice(ctx, number_of_dices, number_of_sides):
dice = [
str(random.choice(range(1, number_of_sides)))
for _ in range(number_of_dices)
]
await ctx.send(', '.join(dice))
@bot.command(name='relaod', help='startet einmal den bot neu')
async def bot_reload(ctx):
await ctx.message.delete()
message = await ctx.send("Starte neu, gib mir 5 Sekunden")
restart_bot()
@bot.command(name='create-voice', help='Erstellt einen Voice channel')
@commands.has_role('Admin')
async def create_channel(ctx, channel_name="Default Channel"):
guild = ctx.guild
existing_channel = discord.utils.get(guild.channels, name=channel_name)
if not existing_channel:
print(f'Erstelle den Spach-channel "{channel_name}"')
await guild.create_voice_channel(channel_name)
if __name__ == "__main__":
bot.run(TOKEN)
一切正常,直到我添加了重新加载/重启功能 之后什么都不起作用了,机器人启动,并调用 on_ready() 函数,但不和谐我不能发出任何命令,如果你需要日志文件让我知道
logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding = 'utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)
def restart_bot():
python = sys.executable()
os.execl(python, python, * sys.argv)
@bot.command(name='relaod', help='startet einmal den bot neu')
async def bot_reload(ctx):
await ctx.message.delete()
message = await ctx.send("Starte neu, gib mir 5 Sekunden")
restart_bot()
@bot.command(name='relaod', help='startet einmal den bot neu')
async def bot_reload(ctx):
await ctx.message.delete()
message = await ctx.send("Starte neu, gib mir 5 Sekunden")
restart_bot()
这是我在停止工作之前添加的所有代码