Im new to python (i learned the basics at a school course),
and for the moment im trying to create simple bots for my discord server.
And for the moment, the bot i really want, is an autodelete bot so that i can say, (for example) delete every message in the ...Channel after 24h. because i really dont wanna do that manually.
I know there are a few good bots that do that,
but for example MEE6 wants me to buy Premium to use the funktion.The other reason why i dont want to use any finished bot is that i really want to understand and learn the code,
i watched many tutorials and tried to put the parts of the scripts that i understood together, but it did not work. I also didnt find a tutorial which explained it to me so that i understood, so now im here and hope that im going to understand it.
I hope there are some ppl to help me. :)
Thanks
-Yami.Code
#bot.event()
async def on_ready(ctx):
while requirement == 1:
await ctx.channel.purge
time.sleep(20)
#the Error is:
line 11, in <module>
#bot.event()
TypeError: event() missing 1 required positional argument: 'coro'
Process finished with exit code 1
You shouldn't call bot.event (remove the parenthesis),
time.sleep is a blocking call, use asyncio.sleep instead (What does "blocking" mean)
on_ready doens't take ctx as an argument, if you want to send a message to a channel you should get the channel object first, and then use the send method
You're missing a parenthesis in your channel.purge method...
import asyncio # if you haven't already
#bot.event
async def on_ready():
channel = bot.get_channel(channel_id) # replace `channel_id` with an actual channel ID
while requirement == 1:
await channel.purge(limit=x) # change `x` accordingly...
await asyncio.sleep(20)
Related
In Discord.py I am currently coding a bot that basically acts as a system for one particular server, and isn't intended to be used as a public bot. But I still wanted to add commands in that makes it simple for administrators to configure the bot inside of Discord, all was well, until I ran into the issue of trying to check if a channel ID is actually correct or not when executing the command, but unfortunately I kept getting error after error after error.
#bot.command()
async def channel(ctx, type, id):
global channel_report
global channel_approve
if id != discord.TextChannel.id:
await ctx.send("Command terminated: bad id.")
return
if type == "report":
#code here
elif type == "approve":
#code here
What have I been missing? I have tried to approach this many different ways, even with methods such as get_message , but got nowhere, and as a newish programmer, but especially new to the Discord API in particular, I'm lost. Thanks for all of those who are dedicating their time to help me, computer coding is just one of them interests.
I'm assuming what you mean by "checking if a channel ID is correct" is that you want to make sure the given ID represents an actual channel that the bot can access. You can use bot.get_channel() to get a channel object from an ID. If the channel doesn't exist, it returns null. So you can just check if it is null.
channel = bot.get_channel(000000000000000000) # try to get a channel which doesn't exist
if channel is None: # will return true as invalid channel returns null
await ctx.send("Command terminated: bad id.")
return
How can a python bot read every message that is sent in a certain channel or read every message that was sent in one using a command like $save 421345187663708161. Thanks in advance, havent been able to find the answer anywhere.
I made this in an on_message Function which scans the message content for "$save" on the beginning, then gets the channel per the given ID.
#client.event
async def on_message(message):
if message.content.startswith("$save"):
splittedcontent = message.content.split()
channel = client.get_channel(splittedcontent[1])
all_messages = channel.history()
What your job now is, is to understand this code, maybe inform about the things that are going on here (like channel.history) and implement this to your code (preferably also with some try/except cases).
I am trying to make a Discord bot that is similar to mee6 in the sense that it counts the messages sent by a user in my Discord server at certain intervals. I have scoured the web and can't find what I'm looking for even though there are similar questions. For example, I was able to find some code that counts the number of messages sent in one specific channel. I was also able to find something that I am basically looking for, which is total messages sent in a guild, but it was written in Java. I hope this narrows my question, and thank you in advance.
You can store the author and the message count in a Dictionary like this:
messageCount = {}
#client.event
async def on_message(ctx):
author = str(ctx.author)
if author in messageCount:
messageCount[author] += 1
else:
messageCount[author] = 1
await client.process_commands(ctx)
So, the dictionary would look something like this:
messageCount = {
'user#1532': 52,
'user#0864': 742,
'user#0067': 662,
...
}
Note: Once the bot goes offline, all the data will be erased, so i'll be a lot safer to store this data in an external file or a database. I'm hoping this answer will give you a gist on how to get started.
I would like to know whether somebody is talking or is AFK.
I have found some examples of things that might help, but I don't quite if it is even supposed to work. Like this one:
async def on_member(member):
if member.voice:
print("HI")
And i would like to know if i have to import something else because currently i only have:
import discord
from discord.ext import commands
The example you provided returns a VoiceState, which you could potentially use. If you have an AFK voice channel and push-to-talk voice channel set up in your guild, you could do something like this:
(Read up about on_voice_state_update() here)
#bot.event
async def on_voice_state_update(member, prev, cur):
user = f"{member.name}#{member.discriminator}"
if cur.afk and not prev.afk:
print(f"{user} went AFK!")
elif prev.afk and not cur.afk:
print(f"{user} is no longer AFK!")
elif cur.self_mute and not prev.self_mute: # Would work in a push to talk channel
print(f"{user} stopped talking!")
elif prev.self_mute and not cur.self_mute: # As would this one
print(f"{user} started talking!")
Clarifying:
Discord doesn't receive audio, unless you're planning on working with the bytes from the socket, but that's quite a sizeable project.
I get nothing showing up that there is an error, but it does not show up in discord. Could someone show me what is wrong?
async def change_status(self):
await client.change_presence(game=Game(name = " ", type = 3))
I would like for the bot to have "listening" or "watching" show up on discord under it's name.
Okay, Update:
(pretty sure this is rewrite)
I figure some people will look this up over time so here it is.
On the discord.py discord server I looked around through #help and found a place where it said the correct answers they just needed to be edited slightly. Here is the answer for it:
await client.change_presence(activity=discord.Game(name="a game"))
This will set the bot to "Playing."
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))
This will set it to "Watching" status.
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))
This will set it to a "Listening" status.
You need the bot to specify when to change the status.
try #client.event this changes the status as the bot comes online.
to change listening to streaming or to watching try changing 2 ( 1,2,3,4 )
As far as i know to use the streaming status feature you need to link your twitch account with your code.
#client.event
async def on_ready():
await client.change_presence(game=Game(name='What ever in here',type = 2))
print('Ready')