Use a webhook to send a message as someone else - python

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()

Related

Discord.py delete message from user

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)

(No errors btw) Why isnt my purge command working?

#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
msg = []
for m in ctx.channel.history():
if m.author == member:
msg.append(m)
await ctx.channel.delete_messages(msg)
heres my code, this is a purge command with discord.py. Im trying to make it so it purges the message of someone you mention. Any help?
The reason this isn't working is because ctx.channel.history() is an async function, so you'll have to use the async keyword before the loop. Like this:
#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
msg = []
async for m in ctx.channel.history():
if m.author == member:
msg.append(m)
await ctx.channel.delete_messages(msg)
You can also just use the purge function with a lambda instead. If you were to go that route, your code would look like this:
#bot.command()
async def purge(ctx, limit=100, member: discord.Member=None):
await ctx.message.delete()
deleted = await ctx.channel.purge(limit=100, check=lambda msg: msg.author == member)
await ctx.channel.send('Deleted {} message(s)'.format(len(deleted)))

Programming a Discord bot in Python- How do I make a kick command?

I want to make a command that kicks a specified user when prompted. Here's what I have:
#bot.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
It doesn't seem to be working, any tips?
Here are two things. Since your on_message events are working using client.event, this means that you should replace your bot.command with client.command as seen below:
#client.command()
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
await user.kick(reason=reason)
kick = discord.Embed(title=f"Kicked {user.name}!", description=f"Reason: {reason}\nBy: {ctx.author.mention}")
await ctx.message.delete()
await ctx.channel.send(embed=kick)
await user.send(embed=kick)
If your kick command still doesn't work, at the very end of your on_message event, you should add await client.process_commands(message). I'll put an example of this below below:
#client.event
async def on_message(message):
if message.content == "Test":
print("recieved")
await client.process_commands(message)

Discord.py rewrite : "Mute" command not working

I am attempting to mute a user.
#client.command()
#commands.has_permissions(administrator=True)
async def mute(ctx, user: discord.Member, *, reason=None):
await user.mute(reason=reason)
await ctx.send(f'{user} has been muted')
I receive the following error:
Member object has no attribute "mute"
How can I mute a user?
Try this:
#client.command()
#commands.has_permissions(administrator=True)
async def mute(ctx, user : discord.Member, *, reason = None):
await user.edit(mute = True, reason = reason)
await ctx.send(f'{user} has been muted')

unban command discord.py rewrite

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)")

Categories