AttributeError: ‘Context’ object has no attribute ‘response’ | discord.py

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

AttributeError: 'Context' object has no attribute 'response' | discord.py

问题

类MyModal(ui.Modal)
    已添加= ui.TextInput(label='你添加了什么', placeholder='## 你添加了什么...', style=discord.TextStyle.long)
    已删除= ui.TextInput(label='你删除了什么', placeholder='## 你删除了什么...', style=discord.TextStyle.long)

类Client(commands.Bot)
    客户端= commands.Bot(command_prefix='!', intents=discord.Intents().all())
    async def on_ready(self)
        打印('已登录为:' + self.user.name)
        打印('Bot ID:' + str(self.user.id))
        打印('Discord版本:' + discord.__version__)
        打印('Python版本:' + str(platform.python_version()))

客户端= Client.client

@client.command(name="modal")
async def modal(interaction: discord.Interaction)
    await interaction.response.send_modal(MyModal)

client.run(TOKEN)
英文:
`import discord
from discord.ext import commands
import time
import json
import platform
from discord import ui


class MyModal(ui.Modal):
    added = ui.TextInput(label='What did you add', placeholder='## What did you add...', style=discord.TextStyle.long)
    removed = ui.TextInput(label='What did you remove', placeholder='## What did you remove...', style=discord.TextStyle.long)


class Client(commands.Bot):

    client = commands.Bot(command_prefix='!', intents=discord.Intents().all())
    async def on_ready(self):
        print('Logged in as: ' + self.user.name)
        print('Bot ID:' + str(self.user.id))
        print('Discord Version: ' + discord.__version__)
        print('Python Version: ' + str(platform.python_version()))


client = Client.client


@client.command(name="modal")
async def modal(interaction: discord.Interaction):
    await interaction.response.send_modal(MyModal)

client.run(TOKEN)`

I followed a youtube tutorial
I didn't write everything the same.

Basically I wanted to achieve a modal in discord when command (with prefix) is typed: 'modal'
I wrote like the guy in the video:

> await interaction.response.send_modal(MyModal())

And because it worked for him, I thought I would work for me too.

I also tried:

> client.tree.command

but then the error was: "Command 'modal' is not found",
Also the async def on_ready(self) isn't working either, but I don't think that matters in this problem. The program is detecting the '!modal' command, but isn't showing the actuall modal.

AttributeError: 'Context' object has no attribute 'response', I don't have context there, so it shouldn't be detected as one. I know that context doesn't have 'response' but I have a interaction there.

  • I need help, because I can't figure it out.

答案1

得分: 0

我看到你的问题,解决起来其实非常容易。

首先,你在~commands.Bot类内部定义了一个~commands.Bot变量。你的bot类应该像这样:

class MyBot(commands.Bot):
    
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)


client = MyBot(...) # 用你要使用的参数和关键字参数替换...

模型类中一切都好,但错误在于响应部分。根据你的问题描述,你尝试使用!modal来调用它,对吗?你必须知道~commands.Context不能将模态框作为响应发送,模态框只能通过~Interaction.response.send_modal来调用(在应用命令和视图中)。
此外,你定义了一个带有discord.Interaction响应的client.command,请将@client.command()替换为@client.tree.command(),在发送时,模态框的title和括号都缺失,修复如下:

@client.tree.command()
async def modal(interaction: discord.Interaction) -> None:
    await interaction.response.send_modal(Modal(title="Your Title"))

请记住,你需要同步命令,这可能需要最多1小时。为了同步它们,我强烈建议使用以下命令:

@client.command()
async def sync(ctx: commands.Context) -> None:
    synced = await client.tree.sync()
    await ctx.reply("已同步 {} 个命令".format(len(synced)))

有任何其他疑问,请在评论中提问。

编辑:

你实际上忘记了模态框上的回调:

class Modal(ui.Modal):
    
    ...

    async def callback(self, interaction: discord.Interaction) -> None:

        embed = discord.Embed(title="模态框结果", description = f"已添加: {self.added.value}\n已移除: {self.removed.value}") # 如果出现错误,可以移除`self.added`和`self.removed`上的`.value`

        await interaction.response.send_message(embeds=, ephemeral=True)
英文:

I read your problem and it's actually very easy to resolve.

First of all, you defined a ~commands.Bot var inside a class of ~commands.Bot. Your bot class must be something like this:

class MyBot(commands.Bot):
    
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)


client = MyBot(...) # replace the ... with the args and kwargs you are going to use

All fine in the modal class but the error is in the response. As your problem description says, you are trying to use !modal to invoke it, right? Well, you must know that ~commands.Context can't send modals as responses, Modals can only being invoked by ~Interaction.response.send_modal (in app commands and in views)
Also, you are defining a client.commandwith an discord.Interaction response, replace the @client.command() with @client.tree.command(), and you are missing the title and the parentheses on the Modal when sending it, fix it like this:

@client.tree.command()
async def modal(interaction: discord.Interaction) -> None:
    await interaction.response.send_modal(Modal(title="Your Title"))

Keep in mind you will need to synchronise the commands and this can take up to 1 hour.
To sync them I strictly recommend to use a command like this:

@client.command()
async def sync(ctx: commands.Context) -> None:
    synced = await client.tree.sync()
    await ctx.reply("Synced {} commands".format(len(synced)))

Any other doubt ask it in the comments

Edit:

You actually forgot the callback on the modal:

class Modal(ui.Modal):
    
    ...

    async def callback(self, interaction: discord.Interaction) -> None:

        embed = discord.Embed(title="Modal Results", description = f"Added: {self.added.value}\nRemoved: {self.removed.value}") # you can remove the `.value` on `self.added` and `self.removed` if error is raised.

        await interaction.response.send_message(embeds=, ephemeral=True)

huangapple
  • 本文由 发表于 2023年2月19日 08:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497264.html
匿名

发表评论

匿名网友

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

确定