I'm trying to delete all roles in my discord server, but it takes a huge amount of time. So I decided to do this task with discord.py bot, but I'm getting this error:
discord.errors.HTTPException: 400 Bad Request (error code: 50028): Invalid Role
Here's my code:
#client.command()
async def delroles(ctx):
for role in ctx.guild.roles:
await role.delete()
The problem is that all users have an "invisible role" called #everyone, which is impossible to remove.
Do:
async def delroles(ctx):
for role in ctx.guild.roles:
try:
await role.delete()
except:
await ctx.send(f"Cannot delete {role.name}")
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='>')
#client.event
async def on_ready():
print("Log : "+str(client.user))
#client.command()
async def delete(ctx):
for role in ctx.guild.roles:
try:
await role.delete()
except:
pass
client.run("token")
Related
Soo i want my bot to send a message when someone join but it is not workin
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
and
i want my bot to take a massage from me and send it to the guy i say but this is not workin too
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
and
here is my WHOLE code:
import os, discord,asyncio
import keep_alive
from discord.ext import commands
client = commands.Bot(command_prefix="!")
token = os.environ.get('Token')
GUILD = os.environ.get('Guild')
#client.event
async def on_ready():
print(f'{client.user} is connected')
#client.command()
async def dm(ctx, member: discord.Member):
await ctx.send('what do u want to say bitch!')
def check(m):
return m.author.id == ctx.author.id
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
keep_alive.keep_alive()
client.run(token)
For making a welcome channel, it is safer to use get_channel instead of get. Because in your code, every time you rename your channel, you need to change your code too, but channel ids cannot be changed until if you delete and create another one with the same name.
Code:
#client.event
async def on_member_join(member):
channel = client.get_channel(YOUR_CHANNEL_ID_GOES_HERE)
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
As for the dm command, I recommend you to get your message as a function parameter. Also you can check when you're DM'ing your bot with the isinstance() function. There is a * before the message parameter though. Its purpose is collecting all of your messages with or without spacing.
Code:
#client.command()
async def dm(ctx, member:discord.Member,*, message):
if isinstance(ctx.channel,discord.DMChannel):
await member.send(f'{ctx.member.mention} has a message for you: \n {message}')
So I found this code for a kick command and I would like to know how to make it so if the person who sent the kick command doesn't have permissions it replies to the user and says that they don't have the right permissions.
#commands.has_permissions(administrator=True)
async def kick(ctx, user : discord.Member,*,reason):
await user.kick(reason=reason)
await ctx.send(f'{user} kicked for {reason} by {ctx.author}')
That's the code
This should send a message telling users that they don't have permissions if they try to use the command without the proper roles
from discord.ext.commands import MissingPermissions
#Import this at the top of your code
#Write the error handler below the code where you define the kick command
#kick.error
async def kick_error(self, ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to kick members.")
For me I would write:
import discord
from discord.ext import commands
from discord.ext.commands import MissingPermissions
client = commands.Bot(command_prefix=("prefix"))
#client.command()
#commands.has_role("Administrator")
async def kick(self, ctx, member : commands.MemberConverter, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked, reason: {reason}")
#kick.error
async def kick_error(self, ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to kick members.")
I would just like to send a DM to my friend via python code.
This is my code, but it does not work.
Code:
import discord
client = discord.Client(token="MY_TOKEN")
async def sendDm():
user = client.get_user("USER_ID")
await user.send("Hello there!")
Your bot might now have the user in its cache. Then use fetch_user
instead of get_user (fetch requests the Discord API instead of
its internal cache):
async def sendDm():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
You can run it with on_ready event:
#client.event
async def on_ready():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
Copy and paste:
import discord
client = discord.Client()
#client.event
async def on_ready():
user = await client.fetch_user("USER_ID")
await user.send("Hello there!")
client.run("MY_TOKEN")
So are you trying to do this with a command? If so here is that
#bot.command()
async def dm(ctx, user: discord.User = None, *, value = None):
if user == ctx.message.author:
await ctx.send("You can't DM yourself goofy")
else:
await ctx.message.delete()
if user == None:
await ctx.send(f'**{ctx.message.author},** Please mention somebody to DM.')
else:
if value == None:
await ctx.send(f'**{ctx.message.author},** Please send a message to DM.')
else:
` await user.send(value)
so, Ive been doing this:
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix='><')
#client.event
async def on_ready():
print("I am ready Winson or not Winson :D")
#client.event
async def on_member_join(member):
channel = client.get_channel(744440768667844698)
message = await channel.send(f"Welcome to HaveNoFaith {member}, happy to be friends with you")
#client.command()
async def ping(ctx):
await ctx.send(f"Your Ping is {round(client.latency *1000)}ms")
#client.command()
async def Help(ctx2):
await ctx2.send("Hi, Im WelcomeBot v1.0...\n\nPrefix: ><\n\nCommands: ping\n help")
#and then Im trying to do like at the message "Welcome etc" if they react with the "check reaction" in that message, they will get a role in the discord server...
you can make a command named addrr(add reaction role), which will look like this -
#client.command()
#commands.guild_only()
#commands.has_permissions(administrator=True)
async def addrr(self, ctx, channel: discord.TextChannel, message: discord.Message, emoji: discord.Emoji,
role: discord.Role):
await ctx.send(f"Setting up the reaction roles in {channel.mention}.")
await message.add_reaction(emoji)
def check1(reaction, user):
return user.id is not self.client.user.id and str(reaction.emoji) in [f"{emoji}"]
while True:
try:
reaction, user = await self.client.wait_for("reaction_add", check=check1)
if str(reaction.emoji) == f"{emoji}":
await user.add_roles(role)
await message.remove_reaction(reaction, user)
else:
await message.remove_reaction(reaction, user)
except:
await message.delete()
break
It will work like this -
><addrr <#channel mention> <message ID> <Emoji> <#Role Mention>
So you can add a reaction to the messages that was sended and use wait_for to wait for a reaction on that message. I recommend you to add the timeout. If you dont want to have this timeout, simply just send these message, save it into a list and in the raw_reaction_add event check if the emoji is the one and the message is one of the messages in your list
As described in the title, I am new to Python(programming in general) and I tried making a bot, however the bot does not respond to commands. I followed/looked through multiple youtube tutorials & articles, but I cannot find a way to fix my problem.
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.event
async def on_message(message):
if message.author == bot.user:
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
Hey why don't you try this instead the same thing but i removed the on message
import discord
from discord.ext.commands import Bot
bot = Bot(".")
#bot.event
async def on_ready():
print("kram is now online")
await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))
#bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
await ctx.send("#everyone")
await ctx.channel.purge(limit = amount)
#bot.command()
async def help(ctx):
await ctx.send("As of right now, .gp is the only working command.")
bot.run("I'm hiding my token")
This should work as when using cogs and when you have a command there is no need to put it in the on_message event. i suggest you watch this series(Really helpful while starting out):
https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg