Denzel
Denzel

Reputation: 389

How to invoke discord function inside the loop

I'm new to discord py and asyncio. I want the bot to run the function five times inside the loop.

from discord.ext import commands
Bot = commands.Bot(command_prefix= '!')
a=0
@Bot.event
async def on_ready():
    print('Bot online')
@Bot.command()
async def msg(ctx):
   global a
   a=a+1
   await ctx.author.send(a)

for i in range(5):
   msg()
Bot.run('token')

Unfortunately this doesn't work without manual entering the command in the channel (!msg). Moreover, it doesn't do it in a loop. How to invoke discord function as standard python function?

Upvotes: 0

Views: 259

Answers (1)

Zetrext
Zetrext

Reputation: 66

You are using an async command without await

for _ in range(5):
    await msg()

But... I highly dout it would work at all because of the way you want to use the msg function better solution is

a = 0
async def send_message_and_increment(ctx):
    global a
    a += 1
    await ctx.send(a) # send to text_channel the user was in
    await ctx.author.send(a) # send to the user (DM) that invoke the command 

@Bot.command()
async def msg(ctx):
   global a

    for _ in range(5):
        await send_message_and_increment(ctx)


Upvotes: 3

Related Questions