How can you make a discord bot that doesn't use commands in Python?
772
I have made a discord bot that uses commands but what I want is for the bot to automatically write a link to a channel on my discord server but I can't do that without using the commands, I have tried many things and nothing, I have searched for tutorials and no one tells me anything
Obviously, the bot can't guess when you want the links to be sent if you don't tell it.
In your particular case, the best way to tell it would be through commands. However, there are alternatives:
1.- Take advantage of an event to send the links. For example, redefining the on_ready() event that is executed when the bot itself is launched. In this way, the bot would send the links simply when connecting.
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='x',intents=discord.Intents.default())
TOKEN = "Tu Token"
guild = None # El server donde quieres que se mande el mensaje
guild_id = 123456789
channel = None # El canal del server donde quieres que se mande el mensaje
channel_id = 987654321
def scrapper():
# Todo lo relacionado con requests y bs4
return ["link de prueba 1", "link de prueba 2"] # los links que quieras enviar
@bot.event
async def on_ready():
global guild, guild_id, channel, channel_id
await bot.wait_until_ready()
# Obtener el canal por el que mandar el mensaje
guild = bot.get_guild(guild_id)
channel = guild.get_channel(channel_id)
# Obtener los links (contenido que tendrá el mensaje)
links = scrapper()
# Mandar el mensaje por el canal
await channel.send("\n".join(links))
print("Conectado")
bot.run(TOKEN)
2.- Periodically send the links using tasks.loop() . The bot will send the links every so often (as indicated). Don't forget to add tasks.Loop.start() in on_ready(), (function_name.start()):
import discord
from discord.ext import commands
from discord.ext import tasks
bot = commands.Bot(command_prefix='x',intents=discord.Intents.default())
TOKEN = "Tu Token"
guild = None # El server donde quieres que se mande el mensaje
guild_id = 123456789
channel = None # El canal del server donde quieres que se mande el mensaje
channel_id = 987654321
links = [] # Ahora guardamos en una variable global los enlaces
def scrapper():
# Todo lo relacionado con requests y bs4
return ["link de prueba 1", "link de prueba 2"] # los links que quieras enviar
@tasks.loop(seconds = 60) # se ejecutará esta función cada 60 segundos
async def enviar_links():
# llama otra vez a la función scrapper para
# recalcular los links si lo consideras necesario
await channel.send("\n".join(links))
@bot.event
async def on_ready():
global guild, guild_id, channel, channel_id, links
await bot.wait_until_ready()
# Obtener el canal por el que mandar el mensaje
guild = bot.get_guild(guild_id)
channel = guild.get_channel(channel_id)
# Obtener los links (contenido que tendrá el mensaje)
links = scrapper()
# Empezamos el bucle en el que se ejecutará la tarea
enviar_links.start()
print("Conectado")
bot.run(TOKEN)
Note that since there is now no "ctx" ( commands.Context ) that you could make use of when using a command, you will now have to manually indicate the channel through which the links should be sent. You can get the channel in the on_ready() event as seen in both examples.
Obviously, the bot can't guess when you want the links to be sent if you don't tell it.
In your particular case, the best way to tell it would be through commands. However, there are alternatives:
1.- Take advantage of an event to send the links. For example, redefining the on_ready() event that is executed when the bot itself is launched. In this way, the bot would send the links simply when connecting.
2.- Periodically send the links using tasks.loop() . The bot will send the links every so often (as indicated). Don't forget to add tasks.Loop.start() in on_ready(), (function_name.start()):
Note that since there is now no "ctx" ( commands.Context ) that you could make use of when using a command, you will now have to manually indicate the channel through which the links should be sent. You can get the channel in the on_ready() event as seen in both examples.