【问题标题】:Can a Discord Bot send to a specific channel without specifying the channel ID each timeDiscord Bot 是否可以在每次不指定频道 ID 的情况下发送到特定频道
【发布时间】:2021-07-06 15:50:40
【问题描述】:

我目前正在尝试通过将 Discord 服务器机器人用于课堂目的来开发互动活动。我对此很陌生,但已经掌握了基本的要点,但我确实有一个问题。我已经能够让我的机器人使用频道 ID 将消息发送到特定的频道,这很棒。问题是这在编码中会很混乱,因为目前我必须为每个单独的频道复制整个代码串。有没有办法把它整理成一组整洁的代码? 目前它看起来像这样:

if (message.channel.id == insertchannelid1):
  channel = client.get_channel(insertchannelid1)
  if message.content.startswith('hello'):
    await channel.send('lets begin')
    await channel.send('I cant seem to see the word hidden here, can you use your flashlight to help me?')
    await message.channel.send(file=discord.File('Spotlight_demo.exe'))
  if message.content.startswith('あか'):
    await channel.send('あか! Thank you! That must have to do with this...')
    

if (message.channel.id == insertchannelid2):
  channel = client.get_channel(insertchannelid2)
  if message.content.startswith('hello'):
    await channel.send('lets begin')
    await channel.send('I cant seem to see the word hidden here, can you use your flashlight to help me?')
    await message.channel.send(file=discord.File('Spotlight_demo.exe'))
  if message.content.startswith('あか'):
    await channel.send('あか! Thank you! That must have to do with this...')

它有效,但正如您所看到的,它们实际上是相同的,除非代码在开始时确定频道 ID。任何帮助整理此内容将不胜感激!

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    您可以将所有目标频道放在一个列表中,并检查频道 ID 是否在所述列表中:

    accepted_channels = [insertchannelid1, insertchannelid2]
    if message.channel.id in accepted_channels:
      if message.content.startswith('hello'):
        await message.channel.send('lets begin')
        await message.channel.send('I cant seem to see the word hidden here, can you use your flashlight to help me?')
        await message.channel.send(file=discord.File('Spotlight_demo.exe'))
      if message.content.startswith('あか'):
        await message.channel.send('あか! Thank you! That must have to do with this...')
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2018-09-22
      • 2021-10-28
      • 2021-01-10
      • 2019-03-18
      • 2018-11-24
      • 2022-01-10
      • 2021-08-18
      • 2020-10-01
      相关资源
      最近更新 更多