Discord.py和Heroku,@commands.slash_command(),如何修复?

gtlvzcf8  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(104)
# scripts/creation.py

import discord
from discord.ext import commands
from role_selection import RoleSelectionView
from data import load_embed, save_data

class Creation(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command()
    async def embed_role_selection(self, ctx):
        """Create role embed"""
        dao_leader_role_id = 1158174122292035764
        if discord.utils.get(ctx.author.roles, id=dao_leader_role_id):
            await ctx.defer()

            embed = load_embed()
            view = RoleSelectionView()

            file1 = discord.File("images/ticketlogo.png", filename="ticketlogo.png")
            file2 = discord.File("images/ruff.jpg", filename="ruff.jpg")

            message = await ctx.send(embed=embed, view=view, files=[file1, file2])

            save_data(message.channel.id, message.id)
        else:
            await ctx.send("You do not have the required role to use this command.", ephemeral=True)
# scripts/daobot.py

import discord
from discord.ext import commands
from role_selection import RoleSelectionView
from creation import Creation
import moderation
from data import load_data
from dotenv import load_dotenv
import os
from monitoring import MonitoringCog
from verification import setup as setup_verification
import nft_command
import embed_generator

load_dotenv()
DISCORD_TOKEN = os.getenv('DAO_BOT_TOKEN')

intents = discord.Intents.default()
intents.presences = True
intents.guilds = True
intents.messages = True
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
async def on_ready():
    data = load_data()
    if data:
        channel = bot.get_channel(data['channel_id'])
        if channel:
            try:
                message = await channel.fetch_message(data['message_id'])
                if message:
                    view = RoleSelectionView()
                    await message.edit(view=view)
            except discord.errors.NotFound:
                print(f"Message with ID {data['message_id']} not found in channel {channel.name}")

bot.add_cog(moderation.Moderation(bot))
creation_cog = Creation(bot)
bot.add_cog(MonitoringCog(bot))
setup_verification(bot)
nft_command.setup(bot)
embed_generator.setup(bot)

bot.run(DISCORD_TOKEN)
2023-10-09T19:21:45.000000+00:00 app[api]: Build started by user [email protected]
2023-10-09T19:22:06.360496+00:00 app[api]: Deploy 7fc3ba6c by user [email protected]
2023-10-09T19:22:06.360496+00:00 app[api]: Release v6 created by user [email protected]
2023-10-09T19:22:07.630260+00:00 heroku[worker.1]: State changed from crashed to starting
2023-10-09T19:22:09.500490+00:00 heroku[worker.1]: Starting process with command `python3 scripts/daobot.py`
2023-10-09T19:22:10.000000+00:00 app[api]: Build succeeded
2023-10-09T19:22:10.048828+00:00 heroku[worker.1]: State changed from starting to up
2023-10-09T19:22:11.141552+00:00 app[worker.1]: Traceback (most recent call last):
2023-10-09T19:22:11.141584+00:00 app[worker.1]: File "/app/scripts/daobot.py", line 6, in <module>
2023-10-09T19:22:11.141603+00:00 app[worker.1]: from creation import Creation
2023-10-09T19:22:11.141620+00:00 app[worker.1]: File "/app/scripts/creation.py", line 8, in <module>
2023-10-09T19:22:11.141651+00:00 app[worker.1]: class Creation(commands.Cog):
2023-10-09T19:22:11.141652+00:00 app[worker.1]: File "/app/scripts/creation.py", line 12, in Creation
2023-10-09T19:22:11.141679+00:00 app[worker.1]: @commands.slash_command()
2023-10-09T19:22:11.141711+00:00 app[worker.1]: ^^^^^^^^^^^^^^^^^^^^^^
2023-10-09T19:22:11.141780+00:00 app[worker.1]: AttributeError: module 'discord.ext.commands' has no attribute 'slash_command'
2023-10-09T19:22:11.309441+00:00 heroku[worker.1]: Process exited with status 1
2023-10-09T19:22:11.327178+00:00 heroku[worker.1]: State changed from up to crashed

我尝试使用firstly @commands.slash_command(),因为从本地所有工作良好,即使与 flask 。当我试图部署到Heroku所有不和谐的机器人存储库时,我开始将所有@commands.slash_command()交换为@commands.command(),但这不是我需要的。
我试着让机器人与斜线命令的开发,与一些经典的“!“命令和web 3钱包验证(如collab.land bot)。
所以Heroku找不到那些@commands.slash_command(),无法想象为什么XD。

xzabzqsa

xzabzqsa1#

你的代码中有一些问题。
1.定义discord.Client和commands.Bot
你应该只使用一个,为了我下面的例子的目的,我将使用commands.bot。

  1. Bot.slash_command不存在。
    斜杠命令使用app_commands.command。
from discord.ext import commands
from discord import app_commands

class MyCog(commands.Cog):
    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot

    @app_commands.command(name="mycommand")
    async def mycommand_callback(self, interaction: discord.Interaction):
        await interaction.response.send_message(content="Hello world")

async def setup(bot: commands.Bot) -> None:
    await bot.add_cog(MyCog(bot))

然后在主文件中:

from discord import commands

bot = commands.Bot(command_prefix="/", ...)

@bot.event
async def on_ready():
    await bot.load_extension("mycog") #  Name of cog file

请参阅有关Cogsloading extensions的文档。
1.通常,不能同时使用斜杠命令和前缀命令。
除非使用混合命令(read more)或on_message事件,否则应仅使用前缀命令或斜杠命令。

相关问题