I was trying to create a bot that delete message from a user with a mute command but i don't know how to do it. My code here.
#bot.command()
async def mute(ctx, member: discord.Member):
#Get the member's id
global user_id
user_id = member.id
#Send a messaage saying the user is muted
await ctx.send(f'{member} muted')
#bot.event
async def on_message(msg):
global user_id
if msg.author.id == user_id:
await msg.delete()
I don't know how to let the event use the variable that created in the command.
When I use the command mute someone
the bot just automatically delete the message sent by that someone
find the solution
user_id = 2
#bot.command()
async def mute(ctx, member: discord.Member):
global user_id
user_id = member.id
await ctx.send(f'{member} muted')
#bot.event
async def on_message(msg):
if msg.author.id == user_id:
await msg.delete()
await bot.process_commands(msg)
Related
I'm making a Discord bot using discord.ext. I want to change the users nickname on the command nick.
For that I wrote following code:
from discord.ext import commands
import discord
# set prefix as "w!"
bot = commands.Bot(command_prefix="w!")
# do stuff when certain error occurs
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.BotMissingPermissions):
await ctx.send("Error: Bot missing permissions.")
if isinstance(error, commands.CommandNotFound):
await ctx.send("Error: The command was not found")
if isinstance(error, commands.MissingPermissions):
await ctx.send("Error: You don't have permission to do that.")
#bot.command()
# check if the user has the permission to change their name
#commands.has_permissions(change_nickname=True)
async def nick(ctx, member: discord.Member, *, nickname):
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
# reset the nickname if no new name was given
#nick.error
async def nick_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
member = ctx.author
await member.edit(nick=None)
await ctx.send(f"Reset nickname to {member.mention}.")
bot.run("TOKEN")
When I enter w!nick test name in Discord it doesn't respond to the message and doesn't change my nickname. But when I just enter w!nick it resets my nickname.
Thanks to #Mr_Spaar for helping.
The solution:
#bot.command()
#commands.has_permissions(change_nickname=True)
async def nick(ctx, *, nickname):
member = ctx.author
await member.edit(nick=nickname)
await ctx.send(f"Nickname was changed to {member.mention}.")
Does this help?
client.command(pass_context=True)
#commands.has_permissions(change_nickname=True)
async def hnick(ctx, member: discord.Member, nick):
await member.edit(nick=nick)
await ctx.send(f'Nickname was changed for {member.mention} ')
I've been looking into this issue, but only found webhooks that copy the author's name and avatar. I'd like to do the same, but with a tagged member's stuff.
trigger:
--say #test test
result:
#test test
This is my code so far:
#bot.command(pass_context=True)
async def say(ctx, *, message: str, member: discord.Member = None):
async with ClientSession() as session:
webhook = discord.Webhook.from_url(WEBHOOK_URL, adapter=discord.AsyncWebhookAdapter(session))
await webhook.send(content=message, username=member.Member.name, avatar_url=member.avatar_url)
Here is a working example of "impersonating" a user through a webhook, it takes the user's name and their profile picture but simply appears as a bot. It's the same method as bots such as "NQN" uses.
#client.command()
async def say(ctx, member: discord.Member, *, message=None):
if message == None:
await ctx.send(
f'Please provide a message with that!')
return
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(
str(message), username=member.name, avatar_url=member.avatar_url)
webhooks = await ctx.channel.webhooks()
for webhook in webhooks:
await webhook.delete()
Try this
#client.command()
async def impersonate(ctx, member: discord.Member, *, message=None):
if message == None:
await ctx.send(f'Who do you want to impersonate?')
return
webhook = await ctx.channel.create_webhook(name=member.name)
await webhook.send(
str(message), username=member.name, avatar_url=member.avatar_url)
await webhook.delete()
msg_dump_channel = 1234
#bot.event
async def on_message(message: discord.Message):
channel = bot.get_channel(msg_dump_channel)
if str(message.author) == "user":
await channel.send(message.content)
await bot.process_commands(message)
This is my code, I know that DMs don't have a guild, so how would you write it for a DM?
this should work for an example
#bot.event
async def on_message(message)
guild = message.guild
if not guild:
print(" DM: {0.author.name} : {0.content}".format(message))
discord.Message objects have a channel attribute that you can use for your check:
#bot.event
async def on_messsage(message)
if isinstance(message.channel, discord.DMChannel) and str(message.author) == 'user':
channel = bot.get_channel(channel_id)
await channel.send(message.content)
else:
pass
bot.process_commands(message)
I need an efficent discord.py command for unban users by their tag, not their name and discriminator. how i can do?
This is the code that I made and it works with *unban name#1234.
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member):
banned_user = await ctx.guild.bans()
member_name, member_discriminator = member.split("#")
for ban_entry in banned_user:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
embed = discord.Embed(title="Fatto!", description=f"Ho sbannato {user.mention}!", color=discord.Color.green())
await ctx.send(embed=embed)
return
How can i make it work with tags? I know you can't directly tag a banned user but with his id you can do it. Thank you for answers!
Hope this helps
from discord.ext.commands import has_permissions
#bot.command()
#has_permissions(ban_members=True)
async def unban(ctx, userId: discord.User.id):
user = get(id=userId)
await ctx.guild.unban(user)
Use the member object as a parameter in your unban function.
Ex:
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member: discord.Member):
await ctx.guild.unban(member)
Then in discord you could execute the command by mentioning the user.
Ex: *unban #aleee
#client.command(description="bans a user with specific reason (only admins)") #ban
#commands.has_permissions(administrator=True)
async def ban (ctx, member:discord.User=None, reason =None):
try:
if (reason == None):
await ctx.channel.send("You have to specify a reason!")
return
if (member == ctx.message.author or member == None):
await ctx.send("""You cannot ban yourself!""")
else:
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
await ctx.guild.ban(member, reason=reason)
print(member)
print(reason)
await ctx.channel.send(f"{member} is banned!")
except:
await ctx.send(f"Error banning user {member} (cannot ban owner or bot)")
I'm trying to get this to check if the user has the role that's tagged or not.
discord.utils.get(ctx.guild.roles, name=rolename)
and
discord.utils.get(user.roles, name=rolename)
#bot.command()
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member, rolename: discord.Role):
role = discord.utils.get(ctx.guild.roles, name=rolename)
if(not role in user.roles):
await user.add_roles(rolename)
embed=discord.Embed(title=f"{user.name} Has been added to a role called: {rolename.name}", color=discord.Color.dark_purple())
await ctx.send(embed=embed)
else:
await ctx.send(f"Hey {ctx.author.name}, {user.name} already has the role called: {rolename.name}")
No errors just keeps giving the role instead of checking if the user has the role or not.
#bot.command()
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
if rolename in user.roles:
await ctx.send("Person already has role")
else:
await user.add_roles(rolename)
await ctx.send("Person doesn't have the role and it has been given to him/her")
Make sure the bot's role is higher than the role you're trying to give the user though because if It's not then you'll get error 50013 Missing Permissions.
This is the fix
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
if rolename not in user.roles:
await user.add_roles(rolename)
await ctx.send("Person doesn't have the role and it has been given to him/her")
else:
await ctx.send("Person already has role")```