0

I am creating a discord bot (.py) and am wondering how to set/change global variables between functions. Code:

team = 0

@bot.command()
async def scrim(ctx, *, team):
    m1 = random.choice(string.ascii_letters)
    m2 = random.choice(string.ascii_letters)
    m3 = random.choice(string.ascii_letters)
    m4 = random.choice(string.ascii_letters)
    m5 = random.choice(string.ascii_letters)
    m6 = random.choice(string.ascii_letters)
    m7 = random.choice(string.ascii_letters)
    category = discord.utils.get(ctx.guild.categories, name="Scrim Partner Hub") 
    sendchannel = await bot.fetch_channel(847285450820812850)
    if ctx.channel == sendchannel:
        ticket = await ctx.guild.create_text_channel(f"{team} scrim", category=category)

        embedVar = discord.Embed(color=0xcfe2f3)
        embedVar.add_field(name=f'{team} scrim.', value='This is a channel made specifically for this scrim. When done, report the score with !report.', inline=False)
        embedVar.add_field(name='Maplist (optional):', value=f'''
Match 1: {m1} Splat Zones
Match 2: {m2} Rainmaker
Match 3: {m3} Tower Control
Match 4: {m4} Clam Blitz
Match 5: {m5} Splat Zones
Match 6: {m6} Rainmaker
Match 7: {m7} Tower Control
''', inline=False)
        return await ticket.send(embed=embedVar)

@bot.command(case_insensitive = True, aliases = ["report", "close"])
async def end(ctx, score, *, team):
        category = discord.utils.get(ctx.guild.categories, name="Scrim Partner Hub")
        sendchannel = await bot.fetch_channel()
        if ctx.channel.catogory == category:
                embedVar = discord.Embed(color=0xcfe2f3)
                embedVar.add_field(name=f'{team} VS _____.', value=f'''
Score: {score}
Team: {team}
        ''', inline=False)
                await sendchannel.send(embed=embedVar)
                return await ctx.channel.delete()

I am hoping to pass a variable to the end command through input on the !scrim command.

0

1 Answer 1

4

You can either use global variables:

var = 0

@bot.command()
async def t1(ctx):
    global var
    var += 1
    await ctx.send(var)


@bot.command()
async def t2(ctx):
    global var
    var += 2
    await ctx.send(var)

or bot instance that will hold your variable:

bot.var = 0

@bot.command()
async def t1(ctx):
    bot.var += 1
    await ctx.send(bot.var)


@bot.command()
async def t2(ctx):
    bot.var += 2
    await ctx.send(bot.var)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.