Мой код для команды префикса:
import discord
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def ping(self, ctx):
message = await ctx.send("Pong!")
latency = self.bot.latency * 1000
await message.edit(content=f'Pong! **{latency:.2f} ms**')
async def setup(bot):
await bot.add_cog(Ping(bot))
Для команды косой черты:
import discord
from discord import app_commands
from discord.ext import commands
class sPing(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command()
async def ping(self, interaction: discord.Interaction):
latency = self.bot.latency * 1000
await interaction.response.send_message(content='Pong!')
msg = await interaction.original_response()
await msg.edit(content=f'Pong! **{latency:.2f} ms**')
async def setup(bot):
await bot.add_cog(sPing(bot))
Есть ли способ перестать повторять это и объединить команду префикса и косую черту в одном фрагменте кода?
Для этого вы можете использовать Гибридные команды.
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command()
async def ping(self, ctx):
message = await ctx.send("Pong!")
latency = self.bot.latency * 1000
await message.edit(content=f'Pong! **{latency:.2f} ms**')
async def setup(bot):
await bot.add_cog(Ping(bot))
Вы можете использовать гибридные команды, которые включают в себя как команды с префиксом, так и команды с косой чертой:
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command(name = "ping", description = "Check the bot's latency")
async def ping(self, ctx):
msg = await ctx.send("Pong!")
latency = self.bot.latency * 1000
await msg.edit(content = f"Pong! **{latency:.2f} ms**")
async def setup(bot):
await bot.add_cog(Ping(bot))
name = "ping"
и description = "Check the bot's latency"
обозначают имя и описание команды. Поэтому, если кто-то не знает, как использовать эту команду, он может прочитать ее использование здесь.
Надеюсь, это поможет!