0
votes

I have created a simple bot using Discord.py which outputs the contents of text files which contain scraped sports fixtures and channel data (scraped data is received from other small scripts I created but haven't included here).

discordbot.py

import discord
import os
from dotenv import load_dotenv


client = discord.Client()

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

client = discord.Client()

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

                
@client.event
async def on_message(message):
    id = client.get_guild(73194****229982)
    

    if str(message.channel) == "boxingmma":
        if message.content.find("!boxing") != -1:

            with open('/home/brendan/Desktop/boxingtest.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()
                    
    if str(message.channel) == "football":
        if message.content.find("!english") != -1:

            with open('/home/brendan/Desktop/splittest.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()
                    
    if str(message.channel) == "football":
        if message.content.find("!scottish") != -1:

            with open('/home/brendan/Desktop/testing2.txt', 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()
            


client.run("NzMxOTQ4Mzk5NzY2NjY3NDg2.XwuPsw.iN****-e2yDnRS_uWqff43Thvqw")

The bot is running fine and when commands are run on the relevant discord channel a DM is sent to the user who sent the request to the bot. The biggest issue I have with this code is the repetitive code in the on_message function. The only difference in the three different double if statements is the channel name e.g. Football or Boxing, the command e.g. !scottish, !english, !boxing and the filepath to the scraped data.

I am curious if anyone knows of anyways this could be improved or a way for the repetitive code to be reused? I tried to pass these as positional arguments from another function, for example:

def callall():
    on_message(channel="football", command="!english", filepath="/home/brendan/Desktop/test.txt")

but it appears that after researching this it is not possible to pass arguments to on_message in discord.py

Any suggestions on how the repetitive code can be resued, or ways in which the general program flow can be improved would be much appreciated

Thanks all.

1
Does it have to be in a on_message event? You could create a command with discord.py's commands framework.MrSpaar
Have you considered using dict and looping over it. example: you can have football as key and /home/brendan/Desktop/splittest.txt as value.SahilM
@Mr_Spaar - it wouldn't have to be limited to on_message event.Brendan Rodgers
@SahilM - that sounds like a great option. Would you be able to outline this in an answer? ThanksBrendan Rodgers

1 Answers

1
votes

you can have a dict as

dict = {"boxingmma":"/home/brendan/Desktop/boxingtest.txt", "football":"/home/brendan/Desktop/splittest.txt", "football1":"/home/brendan/Desktop/testing2.txt"}

for key, value in dict:
    if str(message.channel) == key:
        if message.content.find("!boxing") != -1:

            with open(value, 'r') as file:
                msg = file.read(2000).strip()
                while len(msg) > 0:
                    await message.author.send(msg)
                    msg = file.read(2000).strip()

for third value I put football as football1 because keys cannot be repeated.