有以下代码:bot.pyimport discordfrom discord.ext import commandimport logstickup osimport asynciofrom config import settingsdiscord.utils.setup_logging(level=logging.INFO,
有如下代码:
机器人
import discord
from discord.ext import commands
import logging
import os
import asyncio
from config import settings
discord.utils.setup_logging(level=logging.INFO,root=True)
bot = commands.Bot(command_prefix = settings['prefix'],intents=discord.Intents.all()) # Так как мы указали префикс в settings, обращаемся к словарю с ключом prefix.
@bot.event
async def on_ready():
synced = await bot.tree.sync(guild=discord.Object(id=1283589491298402314))
print(f'Синхронизированно {len(synced)} команд!')
print(bot.guilds)
async def load():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
if filename != '__init__.py':
await bot.load_extension(f"cogs.{filename[:-3]}")
async def main():
await load()
await bot.start(settings['token'])
asyncio.run(main())
utuls.py
import discord
from discord.ext import commands
from discord import app_commands
class UtilsCommand(commands.Cog):
def __init__(self,bot:discord.Client):
self.bot=bot
@app_commands.command(name='ping',description='Check ping')
async def ping(self,interaction:discord.Interaction):
await interaction.response.send_message(content=f"Pong! ({self.bot.latency})")
async def setup(bot):
await bot.add_cog(UtilsCommand(bot))
删除片段 \'guild=discord.Object(id=1283589491298402314)\' 时,命令会全局同步,但我不需要这样做,因为我想在本地同步它们。
控制台中出现一条消息,显示同步了 0 个命令。我找不到语法错误,所以请求帮助。
我在类似的文章中寻找答案,到处都有与我的代码一致的代码来实现目标,但出于某种原因,它对我来说不起作用 :/。甚至没有错误...