When I ban someone with my bot, all recent message get deleted, so I was wonder if is there a way to keep messages.
#bot.command()
#commands.has_permissions(administrator=True)
async def ban(ctx, user: discord.User, *, reason = None):
ban = await ctx.reply(embed = discord.Embed(description = f'{loading_emoji} Banning {user.mention} from `server1` and `server2`...', color = 0x00BF23), mention_author=True)
s1 = await bot.fetch_guild(server1)
s2 = await bot.fetch_guild(server2)
await s1.ban(user, reason = reason)
await s2.ban(user, reason = reason)
await asyncio.sleep(0.9)
await ban.edit(embed = discord.Embed(title = 'Success!', description = f'{shield_emoji} Banned {user.mention} from `server1` and `server2`!', timestamp = datetime.utcnow(), color = 0x00BF23), mention_author=True)
Edit: So i checked and i just needed to add delete_message_days=0 in await s1.ban(user, reason = reason) so should look like this await s1.ban(user, reason = reason, delete_message_days=0) thx for the help
You can use the delete_message_days (int) Parameter in Guild.ban
Api Docs
Related
async def afk(self, ctx, *args):
msg = ' '.join(args)
self.data.append(ctx.author.id)
self.data.append(msg)
await ctx.author.edit(nick=f'[AFK] {ctx.author.name}')
await ctx.send("afk set!")
#commands.Cog.listener()
async def on_message(self, message):
for i in range(len(self.data)):
if (f"<#{self.data[i]}>" in message.content) and (not message.author.bot):
await message.channel.send(f"<#{self.data[i]}> is away right now, they said: {self.data[i+1]}")
return None
break
#commands.Cog.listener()
async def on_typing(self, channel, user, when):
if user.id in self.data:
i = self.data.index(user.id)
self.data.remove(self.data[i+1])
self.data.remove(user.id)
nick = ctx.author.name.replace('[AFK]', '')
await ctx.author.edit(nick=nick)
await channel.send(f"{user.mention}, Welcome back!")
But it is showing ctx is not found nick = ctx.author.name.replace('[AFK]', '') i have been trying various methods to solve it, but i am not able to fix this, pls help me
You're using ctx in a func which doesnt have a ctx arg, you should instead use
nick = user.author.name.replace('[AFK]', '')
await user.author.edit(nick=nick)
I am new to coding so sorry if this is easy to do. But what I am wanting to have my bot do is send a message to my private guild when it is added or removed from another guild. Also if it could say the name of said guild that would help as well.
#client.event
async def on_guild_join(guild):
chn = client.get_channel(YOUR_PRIVATE_GUILD_CHANNEL_ID)
embed = discord.Embed(title=f"Bot Invited",description=f"""
**User Name:** {bot_entry[0].user.name}
**User ID:** {bot_entry[0].user.id}
**Server Name:** {guild.name}
**Server ID:** {guild.id}
**Members Count:** {len(guild.members)}
""", colour=0x2dc6f9)
embed.set_footer(icon_url=guild.icon_url, text=guild.name)
embed.set_author(name=f"{bot_entry[0].user.name}#{bot_entry[0].user.discriminator}", icon_url=bot_entry[0].user.avatar_url)
embed.timestamp = datetime.datetime.utcnow()
server_invite = await guild.text_channels[0].create_invite(max_age = 0, max_uses = 0)
await chn.send(f"{server_invite}", embed=embed)
Something like this?
#client.event
async def on_guild_join(guild):
await client.get_channel(idchannel).send(f"{client.name} has joined")
I do command, that if people click on emoji, create ticket.
#bot.command()
async def ticket(ctx):
embed = discord.Embed(
color = discord.Color.green()
)
embed.set_author(name="Откыть запрос")
embed.add_field(name="To create a ticket react with 📩", value="help")
await ctx.send( embed = embed )
emoji = '📩'
await ctx.channel.purge(limit=1)
message = await ctx.send(embed=embed)
await message.add_reaction(emoji=emoji)
This is message, where people can click on emoji.
And this is code to create channel:
if str(payload.emoji) == "📩":
guild = bot.get_guild(payload.guild_id)
channel = await guild.create_text_channel('Ticket')
guild = message.guild
If someone can, help please to do that the person who created it and the 3 highest roles would have access to the ticket. And aso that the channel name is from 0000 in order to infinity, or only the name of the one who created the ticket
I know, that few people will help, but I hope
guild = bot.get_guild(payload.guild_id)
overwrites = {
guild.me: discord.PermissionOverwrite(view_channel=True),
payload.member: discord.PermissionOverwrite(view_channel=True),
guild.default_role: discord.PermissionOverwrite(view_channel=False)
}
for role in guild.roles[1:4]:
overwrites[role] = discord.PermissionOverwrite(view_channel=True)
channel = await guild.create_text_channel(f"Ticket-{payload.member.display_name}", overwrites=overwrites)
I have made a discord bot in which there is a rob command as follow:-
#client.command(aliases=['rb'])
#commands.cooldown(2, 60, commands.BucketType.user)
async def rob(ctx,member : discord.Member):
if not member == ctx.author and discord.Status.offline and member.bot:
await open_account(ctx.author)
await open_account(member)
bal = await update_bank(member)
if bal[0]<100:
await ctx.send('It is useless to rob them :(')
return
earning = random.randrange(0,bal[0])
await update_bank(ctx.author,earning)
await update_bank(member,-1*earning)
await ctx.send(f'{ctx.author.mention} You robbed {member} and got ֍{earning} ')
else:
await ctx.send('hey stupid, you seem stupid cuz u r either robbing urself or a bot')
I have added the if function wanting to check whether the mentioned member is not the author and offline and a bot but it doesn't work. The command works even on offline users and bots (it doesn't work on the author, but I want it to not work on any of the three)
You can add my bot too to check the feature - https://discord.com/api/oauth2/authorize?client_id=892376302831677520&permissions=8&scope=bot
try this
i have tested this my self it worked so it should also work for you
#client.command(aliases=['rb'])
#commands.cooldown(2, 60, commands.BucketType.user)
async def rob(ctx,member : discord.Member):
if member == ctx.author or ctx.author.status == discord.Status.offline or member.bot:
await ctx.send('hey stupid, you seem stupid cuz u r either robbing urself or a bot')
return
await open_account(ctx.author)
await open_account(member)
bal = await update_bank(member)
if bal[0]<100:
await ctx.send('It is useless to rob them :(')
return
earning = random.randrange(0,bal[0])
await update_bank(ctx.author,earning)
await update_bank(member,-1*earning)
await ctx.send(f'{ctx.author.mention} You robbed {member} and got ֍{earning} ')
Though #idk answer is quite near to it the correct code will be
#client.command(aliases=['rb'])
#commands.cooldown(2, 60, commands.BucketType.user)
async def rob(ctx,member : discord.Member):
if member == ctx.author or member.status == discord.Status.offline or member.bot:
await ctx.send('hey stupid, you seem stupid cuz u r either robbing urself or a bot')
return
await open_account(ctx.author)
await open_account(member)
bal = await update_bank(member)
if bal[0]<100:
await ctx.send('It is useless to rob them :(')
return
earning = random.randrange(0,bal[0])
await update_bank(ctx.author,earning)
await update_bank(member,-1*earning)
await ctx.send(f'{ctx.author.mention} You robbed {member} and got ֍{earning} ')
The main thing is that if member(the user to be robbed) is offline, the code won't work, so it protects them. Here we can add bonus for the victim too
I'm new to discord bot making, and I'd like to have the bot add a reaction to my message that, once pressed, allows the user reacting to pick up a role. What would be the code to allow me to do this? Thank you.
This is a very well written and formatted question! In order for the bot to detect that the reaction is on a specific message, you can do many ways. The easiest way would be to be by ID, so I'm just going to do this with a simple command.
messageIDs = []
#client.event
async def on_raw_reaction_add(payload):
global messageIDs
for messageID in messageIDs:
if messageID == payload.message_id:
user = payload.member
role = "roleName"
await user.add_roles(discord.utils.get(user.guild.roles, name = role))
#client.command()
async def addMessage(ctx, messageID):
global messageIDs
emoji = "👍"
channel = ctx.message.channel
try:
msg = await channel.fetch_message(messageID)
except:
await ctx.send("Invalid Message ID!")
return
await msg.add_reaction(emoji)
messageIDs.append(messageID)