2
votes

I'm currently trying to develop interactive activities through use of Discord server bots for classroom purposes. I am fairly new at this but have got the basic gist of things, I do have a question though. I have been able to make my bot send messages to specific channels using the channel IDs which is great. The problem is that this is going to be messy in coding as at the moment I have to duplicate the entire string of code for each individual channel. Is there a way to tidy this up into one neat set of code? Currently it looks like this:

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...')

It works but as you can see they are literally identical bar the code which determines the channel ID at the beginning. Any help tidying this up would be much appreciated!

1

1 Answers

0
votes

You could put all the target channels in a list, and check if the channel id is in said list:

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...')