discord.py中关于斜杠命令的问题在cogs中

huangapple go评论55阅读模式
英文:

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=&#39;#&#39;, 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(&quot;/ping.py is ready!&quot;)

@client.tree.command(name=&quot;ping&quot;, description=&quot;shows bot ping in ms.&quot;)
async def ping(self, interaction: discord.Interaction):
    bot_latency = round(self, client.latency * 1000)
    await interaction.response.send_message(f&quot;Pong!{bot_latency} ms.&quot;) 


async def setup(client):
    await client.add_cog(ping(client))       

error: TypeError: unsupported type annotation &lt;class &#39;discord.interactions.Interaction&#39;&gt;

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 = &quot;Shows bot current latency in ms.&quot;)
    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&quot;Pong! Bot latency is `{bot_latency}`ms&quot;)

async def setup(client):
    await client.add_cog(Ping(client))

huangapple
  • 本文由 发表于 2023年2月23日 20:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定