How to create automatic messages by Discord bot with constant updates? (python api)

Asked

Viewed 104 times

-1

import discord
import TOKEN_value
import memes

client = discord.Client()
TOKEN = TOKEN_value.token()

memes_ON = False


@client.event
async def on_ready():
    print('BOT HAS BEEN CONNECTED.')
    print(client.user.name)
    print(client.user.id)


@client.event
async def on_message(message):
    global memes_ON
    if message.content.lower().startswith('!memes'):
        memes_ON = True
        await message.channel.send('Now I gonna send memes for you!')

    if memes_ON:
        meme = memes.memes_search()
        if meme != 'no memes here':
            await message.channel.send(meme)


client.run(TOKEN)

The function memes_search() returns me a link from some meme of Imgur, but it is never a repeated link, IE, not always have something new to send, and in this case it returns the string 'no memes here'.

The token() function just returns my token

What I would like to do is that every time I had a new link it was automatically sent to the chat in the place that was typed ! memes, but it stops running if there are no new links and only comes back if someone sends a message on the server. The only way it worked was to send messages endlessly even if they are not links, otherwise it stops running. I’ve tried a lot of things and none of them work. What I need is that the function does not stop running and continue doing the check "if memes_ON", I tried to print some values and saw that from the moment that a link is not returned it runs the function twice and until have new messages on the server.

1 answer

-1

I’m not familiar with the Discord.py API, but maybe it can be implemented that way:

  1. Create a list (or set) that will store all channels in which memes will be sent:
channels = set()
  1. When a channel sends the message "! memes", add the channel ID in channels:
@client.event
async def on_message(message):
    if message.content.lower().startswith('!memes'):
        await message.channel.send('Now I gonna send memes for you!')
        channels.add(message.channel.id)
  1. When a channel sends the message "! stopmemes", for example, remove the channel ID from channels:
@client.event
async def on_message(message):
    if message.content.lower().startswith('!memes'):
        await message.channel.send('Now I gonna send memes for you!')
        channels.add(message.channel.id)

    elif message.content.lower().startswith('!stopmemes'):
        await message.channel.send('Now I gonna stop sending memes for you!')
        channels.discard(message.channel.id)
  1. Create a corrosion that will rotate endlessly, sending memes to the channels on channels:
async def meme_loop():
    while True:
        meme = memes.memes_search()
        if meme == "no memes here":
            continue
  
        for channel_id in channels:
            channel = client.get_channel(channel_id)
            await channel.send(meme)
  1. Run Discord code along with meme code:
# Substitua
#
# client.run(TOKEN)
#
# por
 
import asyncio
await asyncio.gather(client.start(TOKEN), meme_loop())
  • await only runs if it is within an async function. I tried to write asyncio.Gather(client.run(TOKEN), meme_loop()) and it works the bot normal but the function of sending memes does not

  • @Obb You could add prints within the function meme_loop 1) before the while, to see if the function is running; and 2) before the for, to see the result of the variable meme in each iteration? Maybe no meme is being sent by memes.memes_search().

  • Got it, buddy. Thanks a lot! I made a loop inside the main function and a few more changes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.