python 从列表调用时,使用Pycord定义bot.get_channel()时出现问题-打印“None”

crcmnpdw  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(99)

我只是尝试在命令user分配通道ID后从列表中调用它。我执行的所有print()命令都产生正确的ID,包括print(a_string),它正好出现在bot.get_channel调用之前。
我不能理解为什么,但由于某种原因,无论我如何分配变量,它总是返回“无”。
有问题的代码部分在类“EmbedConfirmButton”内部,有问题的变量是“esendChannel”,由代码块底部的斜杠命令赋值。

  • 这是大量的代码,但我想确保包括整个事情的参考,以防万一 *
eEdit = ["blank"]
esendChannel = ["blank"]
eTitle = ["blank"]
eDescription = ["blank"]
channel = []

class EmbedSend(discord.ui.Modal):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        #self.title = kwargs.get("title")
        #self.description = kwargs.get("description")

        self.add_item(discord.ui.InputText(label="Title", value = eTitle[0]))
        self.add_item(discord.ui.InputText(label="Message Content", style=discord.InputTextStyle.long, value = eDescription[0])) 

    async def callback(self, interaction: discord.Interaction):
        eTitle[0] = self.children[0].value
        eDescription[0] = self.children[1].value

        esend = discord.Embed(title=''.join(eTitle), description=''.join(eDescription))
        eEdit[0] = [esend]

        print(eEdit)

        await interaction.response.send_message(embed = esend, view = EmbedConfirmButton(), ephemeral=True)
        


class EmbedConfirmButton(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(label="Send", custom_id="button-embededit", style=discord.ButtonStyle.blurple) 
    async def first_button_callback(self, button, interaction):
        finalembed = discord.Embed(title=eTitle, description=eDescription)
        finalembed.set_image(url="")
        s = [str(integer) for integer in esendChannel]
        a_string = "".join(s)
        print(a_string)
        await bot.get_channel(a_string).send(embed = finalembed)
        await interaction.response.send_message("test", ephemeral = True)
    
    @discord.ui.button(label="Edit", custom_id="button-embededit2", style=discord.ButtonStyle.blurple) 
    async def second_button_callback(self, button, interaction):
        modal = EmbedSend(title=eTitle[0])
        await interaction.response.send_modal(modal)
        await interaction.followup.send("test")

    @discord.ui.button(label="Cancel", custom_id="button-embedcancel", style=discord.ButtonStyle.blurple) 
    async def third_button_callback(self, button, interaction):
        await interaction.response.send_message("Canceled Interaction", ephemeral = True)
        return

@bot.slash_command()
@option("channel")
async def embedsend(ctx, channel:discord.TextChannel):
    modal = EmbedSend(title = "Embed Create")
    await ctx.send_modal(modal)
    await ctx.respond("Success", ephemeral = True)
    esendChannel[0] = str(channel.id)
    print(esendChannel)

我尝试过用几种不同的方法给变量赋值,但我完全不知道为什么只有在传递给bot.get_channel()时它才会返回“None

nimxete2

nimxete21#

您是说当使用a_string调用bot.get_channel时,bot.get_channel返回None吗?
你试过用await bot.fetch_channel(a_string)来代替吗?可能是因为那个通道不在该高速缓存中,所以get_channel没有返回任何东西。

相关问题