2

I have an array I want to send a handful of messages in a for loop with discord.py. I am trying to use the on_ready() command but having a problem with it only sending the first message. I'm fairly new to both async and discord bots. There must be a simpler solution here...

client = discord.Client()
links = []
for x in y:
    # do some things
    links.append(stuff)

@client.event
async def on_ready():
    channel = client.get_channel(12345678910)
    for link in links:
        await channel.send(link)


client.run(DISCORD_TOKEN)

Thanks for any help in advance!

4
  • Can you provide an example of what your array looks like? Commented Sep 3, 2020 at 21:54
  • They are just a bunch of URL strings. ['google.com', 'amazon.com', 'facebook.com'] Commented Sep 3, 2020 at 22:01
  • And you want them to be sent to channel when the bot is ready? Commented Sep 3, 2020 at 22:02
  • Correct, just want to dump them into the channel one by one Commented Sep 3, 2020 at 22:04

2 Answers 2

2

Instead of adding your code under the on_ready() event you could create a loop that runs 1 time once the bot is ready then stops. To create a loop use discord.ext.tasks.

from discord.ext.tasks import loop

@loop(count=1)
async def send_links():
    channel = client.get_channel(730064641857683581)
    links = ['link1', 'link2', 'link3', 'link4']
    for link in links:
        await channel.send(link)


@send_links.before_loop
async def before_send_links():
    await client.wait_until_ready()  # Wait until bot is ready.

@send_links.after_loop
async def after_send_links():
    await client.logout()  # Make the bot log out.


send_links.start()
client.run(DISCORD_TOKEN)
Sign up to request clarification or add additional context in comments.

6 Comments

One last question. My program seems to hang after it completes the task. Any way to have it close after executing. client.logout() seems to be the move but not sure where it goes
What do you mean by hang?
The program doesn't terminate after posting the links. I want to run this in cron so don't want a ton of hanging processes.
You want the bot to post the links then logout?
Beautiful. Thank you so much! (A bit embarrassed I couldn't figure that part out :P )
|
0

At first, client = discord.Client() is absolutely not good for defining client. You should define the client with

client = commands.Bot(command_prefix='command's prefix here'). Then if you want to make it a command, you can do this:

@client.command()
async def send_link(ctx):
    for link in links:
        await ctx.send(link)

But that's not good because it'll send so many messages, so I'd rather to use embeds:

async def send_link(ctx):
    embed = discord.Embed()
    for link in links:
        embed.add_field(name=" ", value=link, inline=False)
    await ctx.send(embed=embed)

You shouldn't make it in on_message because that's meaningless. And in the code, you did channel = client.get_channel(1234667890). That's also a problem, you have to change it with a real channel id.

6 Comments

Thank you for your help. Working on some changes. I have the right id in the real code. Part of what I am trying to do is not have a prompt for the messages as I plan to run it in cron
What does ctx represent and also will the send_link function be called in client.run(DISCORD_TOKEN) ?
You should read some documentation, for example api references. send_link is a command that runs when you type prefix + send_link in chat, like .send_link. Ctx is getting the channel that message sent and sending the messages in that channel. So with these codes, if you define the command_prefix in the commands.Bot then type prefix + send_link in your guild, it will send the array data.
That makes sense, but I am trying to have it send without being prompted. I can't seem to find this sort of example in the docs.
Do you want it to be sent just once?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.