I am trying to make the bot send a message every minute in discord.py. I do acknowledge that this is a easy thing to do but I have tried multiple times but resulting with no luck. I have not gotten any error messages.
Here is my code:
import discord
from discord.ext import tasks
client = discord.Client()
#tasks.loop(minutes=1)
async def test():
channel = client.get_channel(CHANNEL_ID)
channel.send("test")
test.start()
client.run(TOKEN)
You try to get channel with get_channel(), but your bot is not ready yet. So you get NoneType object, not channel. Try to this solution:
#tasks.loop(minutes=1)
async def test():
channel = client.get_channel(CHANNEL_ID)
await channel.send("test")
#client.event
async def on_ready():
test.start()
Related
I am trying to loop through message history of a particular Discord channel and delete all previous messages before sending new messages to the channel. I can't seem to figure this out and have searched EVERYWHERE to find a solution. My code below:
import discord
from discord.ext import tasks, commands
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
async def my_background_task():
channel = client.get_channel(999999999999999)
history = await channel.history().flatten()
await channel.delete_messages(history)
# Loop and send messages to channel
while not client.is_closed():
await channel.send('Test')
schedule.every(5).seconds.do(my_background_task)
# Loop event
#client.event
async def on_ready():
print('Running...')
client.loop.create_task(my_background_task())
When I go to run my code, I get the following error:
repl process died unexpectedly: signal: killed
You can try using the channel.purge() methode instead. It Works fine for me. The only Problem is that it will be a bit laggy if there are too many messages in the channel.
import discord
from discord.ext import tasks, commands
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#deletes all messages in one particular channel
async def deleteAllMessages(deleted, channel):
purge = len(await channel.purge(limit=100))
if purge < 100:
print(f"process finished\n{deleted+purge} messages were deleted.")
return
else:
await deleteAllMessages(deleted+100, channel)
return
async def my_background_task():
channel = client.get_channel(999999999999999)
await deleteAllMessages(0, channel)
#Loop and send messages to channel
if not client.is_closed():
await channel.send('Test')
schedule.every(5).seconds.do(my_background_task)
# Loop event
#client.event
async def on_ready():
print('Running...')
client.loop.create_task(my_background_task())
I hope this is what you were looking for
Here's my code but it seems like it doesn't work. I'm so sorry but, I'm still a newbie but, I would very much appreciate your help and critics.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
intents=discord.Intents.all())
#client.event
async def on_message_join(member):
channel = client.get_channel(channelid)
count = member.guild.member_count
embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count)
embed.set_thumbnail(url=member.avatar_url)
await channel.send(embed=embed)
time.sleep(5)
message.delete(embed)
The correct discord event to catch that a person joins your discord is:
async def on_member_join(member: discord.Member):
rather than on_message_join
To easily delete the message you can first make it a string:
msg = await channel.send(embed=embed)
then get it's id by:
msg_id = msg.id
then fetch it:
msg_todel = await channel.fetch_message(int(msg_id))
then delete it:
await msg_todel.delete()
Just use delete_after=seconds, this exactly what's your wanted
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
intents=discord.Intents.all())
#client.event
async def on_message_join(member):
channel = client.get_channel(channelid)
count = member.guild.member_count
embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count)
embed.set_thumbnail(url=member.avatar_url)
await channel.send(embed=embed, delete_after=5)
According to the discord.py docs, you could edit the message object after 5 seconds and then just set the new embed parameter to None, this seems to be what you're after here.
Below is a way you can alter your code to do this.
import discord
import asyncio # You will need to import this as well
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
intents=discord.Intents.all())
#client.event
async def on_message_join(member):
channel = client.get_channel(channelid)
count = member.guild.member_count
embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count)
embed.set_thumbnail(url=member.avatar_url)
message_object = await channel.send(embed=embed) # You can save the message sent by attaching it to a variable. You don't need to send more calls to refetch it.
'''
time.sleep(5) <--- You should not use time.sleep() as it will stop your entire bot from doing anything else during these 5 seconds.
Use asyncio.sleep() as a better alternative. Does the same thing as time.sleep() but other functions of your bot will still function, this is called a blocking function.
'''
asyncio.sleep(5)
await message_object.edit(embed = None)
Unless you want the entire message deleted, then you can just use delete_after in order to obtain this.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=prefix,
intents=discord.Intents.all())
#client.event
async def on_message_join(member):
channel = client.get_channel(channelid)
count = member.guild.member_count
embed=discord.Embed(title=f"Welcome to {member.guild.name}", description=f"Hello there {member.name}!", footer=count)
embed.set_thumbnail(url=member.avatar_url)
await channel.send(embed=embed, delete_after = 5) # Just add this parameter at the end.
I am brand new to making discord bots, and I'm wanting to make a bot that sends coding challenges for 5 different languages (python, java, javascript, c#, and html). I have set the channels up in my test discord, and I've given the bot admin to be able to read and write in channels, etc. I am trying to figure out how to get it to be on a 24 hour timer, and when that timer reaches zero, it sends out the code challenges for each language to it's corresponding discord channel.
import random
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
TOKEN = ''
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
#bot.command()
async def hello(ctx, member: discord.member):
await ctx.send(f"Hello {member.name}")
#bot.command()
async def
bot.run(TOKEN)
so far I've been able to bring the bot online, and I've learned how to give it custom commands, but I have zero idea on where to start for what I'm trying to do, and any advice or tips would be greatly appreciated :) TYIA
You can use discord.ext.tasks for a loop that runs every 24 hours. If you have the guild object you can use for channel in guild.channels: and then send the message. You can get the guild object with the id: bot.get_guild(id). All together it would look like this:
import random
import discord
from discord.ext import commands, tasks # Import the tasks module
bot = commands.Bot(command_prefix='!')
TOKEN = ''
#bot.event
async def on_ready():
print(f'{bot.user.name} has connected to Discord!')
challenge.start() # Start the loop
#bot.command()
async def hello(ctx, member: discord.member):
await ctx.send(f"Hello {member.name}")
#tasks.loop(seconds=86400) # Loops every 24h
async def challenge():
guild = await bot.get_guild(id) # Gets the guild object by the id
for channel in guild.channels: # Runs a command in every channel
await channel.send(message) # Says the message
bot.run(TOKEN)
I did not test this and it may not work.
I have been trying to create a bot for Discord using the discord.py library however when I am running the program it is not sending the message as expected. It is a simple bot that is suppose to send a message every 10 minutes to a channel. I am not getting any error messages in the command line and can't seem to see any obvious errors? Any help would be greatly appreciated.
import asyncio
client = discord.Client()
async def my_background_task():
await client.wait_until_ready()
counter = 0
channel = discord.Object(id='my channel ID goes here')
while not client.is_closed:
counter += 1
await message.channel.send("TEST")
await asyncio.sleep(5) # task runs every 60 seconds
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.loop.create_task(my_background_task())
client.run('My token goes here')
Although creating a task could work, I wouldn't recommend it when there are better options. Dpy's command.ext addon has a task based system built in directly, so let's look into using that instead.
import discord
from discord.ext import tasks
client = discord.Client()
#tasks.loop(minutes=10)
async def my_background_task():
"""A background task that gets invoked every 10 minutes."""
channel = client.get_channel(754904403710050375) # Get the channel, the id has to be an int
await channel.send('TEST!')
#my_background_task.before_loop
async def my_background_task_before_loop():
await client.wait_until_ready()
my_background_task.start()
client.run('Your token goes here')
The time until which the above loop will run is dependent upon human psychology, laws of energy and cosmos.
That is:
• You get bored of it
• The power goes down and your script stops working
• The universe explodes
Read more about it here:
https://discordpy.readthedocs.io/en/latest/ext/tasks/
If the channel ID is a string, It should be an integer.
Turn this:
channel = discord.Object(id='my channel ID goes here')
into:
channel = discord.Object(id=101029321892839) # an example ID
If that doesn't solve your problem, try setting 'message.channel.send' to 'channel.send' and client.is_closed should be
client.is_closed() # put brackets
Instead of client.loop.create_task(my_background_task()), just put the background function in the on_ready event. And take out await client.wait_until_ready() and just put the function in the on_ready event as well.
I have also changed the client variable, taken out unnecessary code, and added some modules.
import asyncio, discord
from discord.ext import commands
client = commands.Bot(command_prefix = ".") # If you don't want a prefix just take out the parameter.
async def my_background_task():
channel = discord.Object(id='my channel ID goes here')
while True:
await channel.send("TEST")
await asyncio.sleep(5) # task runs every 60 seconds
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
my_background_task()
client.run('My token goes here')
Also in future when posting a question, please tag the python version as I don't know which version it is and then this and other answers could be wrong.
it may hard to me, but i believe on stackoverflow power,
i need to put a number as bot status on my bot.
Here : http://gamers-control-2.000webhostapp.com/count.txt
Also Pic of it : Picture
#time to show status = text that founded in blank website = "41"
#http://gamers-control-2.000webhostapp.com/count.txt
#some how, you say i can type the number, i say the number in web it
change everytime, so i need to get that number to show it as bot status.
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from discord import Game
Client = discord.client
client = commands.Bot(command_prefix = '!')
Clientdiscord = discord.Client()
#client.event
async def on_ready():
await client.change_presence(game=discord.Game(name=http://gamers-control-2.000webhostapp.com/count.txt, type=3))
print('test ready')
client.run("....")
im new with discord.py
You need to first import requests at the beginning
import requests
Then before the ready event
count = requests.get('http://gamers-control-2.000webhostapp.com/count.txt')
And then you set it to
await client.change_presence(game=discord.Game(name=count.text, type=3))
I use await bot.change_presence() like this:
NOTICE:
I am leaving out part of my code
#bot.event
async def on_ready():
replit.clear()
print(f'Logged in as {bot.user.name} - {bot.user.id}')
bot.remove_command('help')
await bot.change_presence(status=discord.Status.online, activity=discord.Game("mod help"))
This is what i use
async def on_ready():
print(f'We have logged in as {bot.user}')
print('Ready!')
return await bot.change_presence(activity=discord.Activity(type=3, name='Add status here'))```