【问题标题】:How do I make my discord bot send this message to a specific channel?如何让我的 discord 机器人将此消息发送到特定频道?
【发布时间】:2023-01-17 23:22:01
【问题描述】:

这是我的代码,我需要它来将响应发送到特定频道。似乎无法在 API 的任何地方找到如何做

@bot.tree.command(name='training', description='Set up a training date & announce it to everyone in the server')

@app_commands.describe(date = "Date")
@app_commands.describe(time = "Time")
@app_commands.describe(location = "Location")

async def say(interaction: discord.Interaction, date: str, time: str, location: str):
  await interaction.response.send_message(f"{interaction.user.mention} is training at {location} on {date} at {time}")```


Ive tried changing the "interaction.response.send_message" into "channel.send" but that gave back an error with the atr: send

【问题讨论】:

    标签: python discord


    【解决方案1】:

    您需要在某处拥有频道 ID - 然后在调用交互时获取该频道对象并然后发送消息。

    文档:

    MY_CHANNEL_ID = 123455678
    
    @bot.tree.command(name='training', description='Set up a training date & announce it to everyone in the server')
    @app_commands.describe(date = "Date")
    @app_commands.describe(time = "Time")
    @app_commands.describe(location = "Location")
    
    async def say(interaction: discord.Interaction, date: str, time: str, location: str):
        my_channel = await interaction.guild.fetch_channel(MY_CHANNEL_ID)
        await my_channel.send(f"{interaction.user.mention} is training at {location} on {date} at {time}")```
    
        # make sure we notify the user that we did what they wanted to do and respond
        await interaction.response.send_message("Done the thing", ephemeral=True)
    
    

    要获取频道 ID,请关注this guide 并右键单击您要将消息发送到的频道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 2021-10-28
      • 2019-03-27
      • 2022-01-23
      • 2021-06-17
      • 2020-10-26
      相关资源
      最近更新 更多