首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不和谐的机器人每次不指定频道ID就能发送到特定的频道吗?

不和谐的机器人每次不指定频道ID就能发送到特定的频道吗?
EN

Stack Overflow用户
提问于 2021-04-11 09:52:34
回答 1查看 312关注 0票数 2

目前,我正试图通过使用不和谐的服务器机器人来开发互动活动,以供课堂使用。我在这方面还是比较新的,但我有基本的要旨,但我还是有一个问题。我已经能够使我的机器人发送消息到特定的通道使用通道is,这是很好的。问题是,这将是杂乱无章的编码,因为目前,我必须重复整个字符串的代码为每个单独的通道。有办法把这些整理成一组整洁的代码吗?目前的情况如下:

代码语言:javascript
运行
复制
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。任何帮助清理这将是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-11 10:10:49

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

代码语言:javascript
运行
复制
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...')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67043575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档