英文:
discord.py problem with slash command in cogs
问题
error: TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>
错误:TypeError: 不支持的类型注解 <class 'discord.interactions.Interaction'>
help pls, i dont even close understand where is the problem
请帮忙,我几乎不理解问题出在哪里。
英文:
this is should be slash command about bot ping but i didnt understand how write slahs commands in cogs
import discord
from discord.ext import commands
from discord import Interaction
client = commands.Bot(command_prefix='#', intents=discord.Intents.all())
class ping(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
await client.tree.sync()
print("/ping.py is ready!")
@client.tree.command(name="ping", description="shows bot ping in ms.")
async def ping(self, interaction: discord.Interaction):
bot_latency = round(self, client.latency * 1000)
await interaction.response.send_message(f"Pong!{bot_latency} ms.")
async def setup(client):
await client.add_cog(ping(client))
error: TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>
help pls, i dont even close understand where is the problem
答案1
得分: 0
Your command is out of the cog and also, you are using client.tree.command
, that decorator is only available outside cogs, in the main file. For cogs must use app_commands.command
decorator.
Also, try to use cog classes outside the main file to prevent errors.
import discord
from discord.ext import commands
from discord import app_commands
from discord import Interaction
# This must be in your main file, not in the cog one
client = commands.Bot(..)
# This must be in another file
class Ping(commands.Cog):
# __init__ is unnecessary, the class auto applies the client parameter
@commands.Cog.listener()
async def on_ready(self):
...
@app_commands.command(description="Shows bot current latency in ms.")
async def ping(self, interaction: discord.Interaction):
bot_latency = round(self.client.latency * 1000) # here you can also use interaction.client.latency instead of self.client.latency
await interaction.response.send_message(f"Pong! Bot latency is `{bot_latency}`ms")
async def setup(client):
await client.add_cog(Ping(client))
英文:
Your command is out of the cog and also, you are using client.tree.command
, that decorator is only available outside cogs, in the main file. For cogs must use app_commands.command
decorator.
Also, try to use cog classes outside the main file to prevent errors.
import discord
from discord.ext import commands
from discord import app_commands
from discord import Interaction
# This must be in your main file, not in the cog one
client = commands.Bot(..)
# This must be in another file
class Ping(commands.Cog):
# __init__ is unnecessary, the class auto applies the client parameter
@commands.Cog.listener()
async def on_ready(self):
...
@app_commands.command(description = "Shows bot current latency in ms.")
async def ping(self, interaction:discord.Interaction):
bot_latency = round(self.client.latency * 1000) # here you can also use interaction.client.latency instead of self.client.latency
await interaction.response.send_message(f"Pong! Bot latency is `{bot_latency}`ms")
async def setup(client):
await client.add_cog(Ping(client))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论