Warm tip: This article is reproduced from serverfault.com, please click

Python. Code repeats several times because the command to execute it is different

发布于 2020-12-31 12:06:06

I have patched up a Discord bot using Python and it is working fine already but there is one problem.

This code repeats itself several times, once for each category of posting and right now I have random, funny, sports, games and news, so you can see the redundancy it is and what it'll further become, if there's need to make more categories.

if '!random' in messageContent:
                channel = int(chanRandom.strip('"'))
                channel = client.get_channel(channel)
                while c < 50:
                    if messageContent == '!random':
                        submission = next(x for x in randomList[sortListHot] if not x.stickied)
                        sortType   = 'Hot sorting'
                    elif messageContent == '!random top':
                        submission = next(x for x in randomList[sortListTop] if not x.stickied)
                        sortType   = 'Top sorting'
                    elif messageContent == '!random new':
                        submission = next(x for x in randomList[sortListNew] if not x.stickied)
                        sortType   = 'New sorting'
                    with open(urlFile, 'r') as urlRead:
                        if str(submission.url) not in urlRead.read():
                            await channel.send(f"{submission.url}\n{submission.title}\n<https://reddit.com{submission.permalink}>")
                            await channel.send("-------------------------")
                            with open(urlFile, 'a+') as urlWrite:
                                urlWrite.write(str(f'{submission.url}\n'))
                            c += 2
                        else:
                            print(f'{messageContent} repost: {submission.url}')
                await channel.send(sortType)

My idea right now is to create a list with every possible command but the problem is posting in the correct channel with the variable channel = int(chanRandom.strip('"')), which changes in accord to the command used channel = int(chanNews.strip('"')) and so on.

There is also an issue with memory usage, because I think the bot is saving everything but there is no need to but this is something for another time.

Any help is appreaciated.

Questioner
HASJ
Viewed
0
Diggy. 2020-12-31 22:45:10

I can't see the contents of all the variables here, but if you want to cut down on code repition, I'd recommend making a dictionary and mapping different variables to the user input.

Here is the idea of it:

my_mapping = {
    "!random": (sortListHot, "Hot sorting"),
    "!random top": (sortListTop, "Top sorting"),
    "!random new": (sortListNew, "New sorting")
}

sorting_type = my_mapping[messageContent.lower()][0]  # sortListHot/Top/New
sorting_text = my_mapping[messageContent.lower()][1]  # "Hot/Top/New sorting"

submission = next(x for x in randomList[sortingType] if not x.stickied)

Having these mapped to a dictionary will also allow for more entries to be added easily and flexibly (just add a new key with the value being a tuple following the current pattern).

The .lower() was added for case insensitivity.

I can also recommend looking into command decorators instead of using the on_message event.


References: