【发布时间】: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