Joseph McNamara
Joseph McNamara

Reputation: 3

How can I make this code shorter (copy/pasted the same lines many times over)?

So I have some piece of code that looks like this:

import discord, asyncio

client = discord.Client()

@client.event
async def on_member_update(preupd, postupd):
    if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('5')).read_messages == True:
        embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
        embed.add_field(name="š¯…³š¯…³Test", value="Test <#5>")
        await client.send_message(client.get_channel('5'), embed=embed)
    if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('6')).read_messages == True:
        embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
        embed.add_field(name="š¯…³š¯…³Test", value="Test <#6>")
        await client.send_message(client.get_channel('6'), embed=embed)
    if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('7')).read_messages == True:
        embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
        embed.add_field(name="š¯…³š¯…³Test", value="Test <#7>")
        await client.send_message(client.get_channel('7'), embed=embed)
    if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel('8')).read_messages == True:
        embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
        embed.add_field(name="š¯…³š¯…³Test", value="Test <#8>")
        await client.send_message(client.get_channel('8'), embed=embed)

client.run('ClientTokenHere')

As you can see, I copy/pasted almost the same thing 4 times, but with slightly different details. So I tried to make the code shorter by making a function like this:

import discord, asyncio

client = discord.Client()

@client.event
async def on_member_update(preupd, postupd):
    async def shorter(numba):
        if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
            embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
            embed.add_field(name="š¯…³š¯…³Test", value="Test <#%s>" % numba)
            await client.send_message(client.get_channel(str(numba)), embed=embed)

    shorter('5')
    shorter('6')
    shorter('7')
    shorter('8')

client.run('ClientTokenHere')

However, this gives me an error: myfile.py:35: RuntimeWarning: coroutine 'on_member_update.<locals>.perms' was never awaited, even though if I run the "longer" version code at the top, everything runs fine smoothly. Obviously I am defining the function incorrectly somehow. How can I fix this? Or is there a better way? Thanks

Upvotes: 0

Views: 97

Answers (1)

Vitalie Maldur
Vitalie Maldur

Reputation: 591

You can also do something like this:

import discord, asyncio

client = discord.Client()

@client.event
async def on_member_update(preupd, postupd):
    for numba in ('5', '6', '7', '8'):
        if postupd.server.id == '1234' and postupd.permissions_in(client.get_channel(str(numba))).read_messages == True:
            embed = discord.Embed(title="š¯…³š¯…³Test", colour=0xFF000)
            embed.add_field(name="š¯…³š¯…³Test", value="Test <#%s>" % numba)
            await client.send_message(client.get_channel(str(numba)), embed=embed)

client.run('ClientTokenHere')

Upvotes: 1

Related Questions