import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import random
from PIL import Image
Client = discord.Client()
client = commands.Bot(command_prefix = "-")`
#client.command
async def spam(ctx, arg):
count = 0
await Context.send(message.channel, "Wait for it....")
time.sleep(3)
while count < 20:
await ctx.send(arg)
time.sleep(3)
count = count + 1
This code is supposed to mention the person specified in the argument. For example if someone typed -spam #Bob the bot should say
#Bob
#Bob 20 times
There's no need to instantiate both discord.Client and commands.Bot. Bot is a subclass of Client.
Bot.command is a function that returns a decorator, not itself a decorator. You need to call it to use it to decorate a coroutine:
#client.command()
You should probably be using a converter to acquire the user you're pinging.
Context is the class that ctx is an instance of. You should be accessing all of the methods through ctx.
Don't use time.sleep, as it blocks the event loop. Await asyncio.sleep instead.
from discord.ext.commands import Bot
from asyncio import sleep
from discord import User
client = Bot(command_prefix = "-")
#client.command()
async def spam(ctx, user: User):
await ctx.send("Wait for it....")
await sleep(3)
for _ in range(20):
await ctx.send(user.mention)
await sleep(3)
client.run("token")
Related
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.
How can I send a message every x time with a discord bot with discord.py?
Here is my code:
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = 'xxxxxxxxxxx'
bot = commands.Bot(command_prefix='!',help_command=None,activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping(channel):
print("is the function being called")
channel = bot.get_channel(877115461014282320)
await channel.send('embed=embed')
bot.run(TOKEN)
Edit: I know I shouldn't leak my key but I edited it after 10 seconds and removed a letter why could someone still access it?
You have to start task using ping.start() in event on_ready or in some command.
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping():
print("is the function being called")
#channel = bot.get_channel(877115461014282320)
#await channel.send('embed=embed')
#bot.event
async def on_ready():
print('on ready')
ping.start()
bot.run(TOKEN)
You have also ping.stop(), ping.cancel(), etc. See doc: tasks
Basically whenever I run this code it gives me an error, I am trying to make the code server mute one of my friends every 20 seconds for 5 seconds, it says there is something wrong with the line where the user is being defined.
import discord
from discord.ext import commands
from discord.ext import commands, tasks
import random
import datetime
from datetime import date
import calendar
import time
import asyncio
from discord.ext.tasks import loop
client = commands.Bot(command_prefix='.')
#tasks.loop(seconds=20)
async def mute_person():
user = await discord.Guild.fetch_member("670549896247509002", "670549896247509002") # Get the Member Object
await user.edit(mute=True) # Mute
await asyncio.sleep(20) # Waits for 20 seconds then unmute.
await user.edit(mute=False) # Unmute
#client.event
async def on_ready():
print("I am ready")
mute_person.start()
client.run("Token")
You are not using the right function discord.Guild doesn't exist as you are using it. the main use must be await guild.fetch_member(member_id) and then guild means the guild object which has a specific ID as well. So you need to get the guild first.
I want to add a username or id to the code so that when I (or somebody else) does .d #username, the selfbot does the whole ".accept" and "u 4" response.
Code:
import discord
import os
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
client = discord.Client()
b = Bot(command_prefix = ".d")
#b.event
async def on_ready():
print("rdy")
#b.event
async def on_message(message)
if message.content == ".d":
await message.channel.send(".accept")
await message.channel.send("u 4")
b.run(os.getenv("TOKEN"), bot = False)
You should use commands it makes things alot easier.
#b.command(aliases=['.d'])
async def d(ctx, member: discord.Member):
await ctx.send(".accept")
await ctx.send("u 4")
I added the .d in aliases cause you cannot have a function starting with . . Here is the link for more information. https://discordpy.readthedocs.io/en/latest/ext/commands/api.html?highlight=group#commands
How can i make presences loop? I want it to change presence every 5-10 sec.
client = commands.Bot(command_prefix=commands.when_mentioned_or(","))
async def presenced():
presences = ["Prefix: ,", "Over people!"]
activity = discord.Activity(
name=random.choice(presences), type=discord.ActivityType.watching)
await client.change_presence(activity=activity)
client.loop.create_task(presenced())
If you didn't already find an answer I got something that should work.
import discord
from discord.ext import commands
import asyncio
import random
client = commands.Bot(command_prefix=commands.when_mentioned_or(","))
async def on_ready():
print("client is online!") # you can add other things to this on_ready listener
client.loop.create_task(presenced()) # start the presenced task when the bot is ready
client.add_listener(on_ready)
async def presenced():
while True:
presences = ["Prefix: ,", "Over people!"]
await client.change_presence(activity=discord.Activity(name=random.choice(presences), type=discord.ActivityType.watching))
await asyncio.sleep(10) # you can of course add some randomizer to this delay
client.run("bot token here :)")