Nuke command in discord.py - python

I've made a nuke command in my discord bot, but I have 2 problems with it. First is how can i send a message to just created channel, and second is how can I make channel in same position as "original".
Here's my code:
# nuke
#client.command()
#commands.has_permissions(ban_members=True)
async def nuke(ctx):
embed = discord.Embed(
colour=discord.Colour.blue,
title=f":boom: Channel ({ctx.channel.name}) has been nuked :boom:",
description=f"Nuked by: {ctx.author.name}#{ctx.author.discriminator}"
)
embed.set_footer(text=f"{ctx.guild.name} • {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
await ctx.channel.delete(reason="nuke")
await ctx.channel.clone(reason="nuke")
await ctx.send(embed=embed)

You can store the cloned channel in a variable:
# nuke
#client.command()
#commands.has_permissions(ban_members=True)
async def nuke(ctx):
embed = discord.Embed(
colour=discord.Colour.blue,
title=f":boom: Channel ({ctx.channel.name}) has been nuked :boom:",
description=f"Nuked by: {ctx.author.name}#{ctx.author.discriminator}"
)
embed.set_footer(text=f"{ctx.guild.name} • {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
await ctx.channel.delete(reason="nuke")
channel = await ctx.channel.clone(reason="nuke")
await channel.send(embed=embed)
The channel should be created in the same position as the original one, if not:
pos = ctx.channel.position
await ctx.channel.delete()
channel = await ctx.channel.clone()
await channel.edit(position=pos)

Related

How to make a set logging channel command

Im trying to make a public bot, and i would want this ticket system witha logging channel it already has a logging channel but how can i modify this to make a command that sets the logging channel?
# --- Ticket Open ---
#client.command()
#commands.guild_only()
async def ticket(ctx):
if ctx.channel.type != discord.ChannelType.private:
channels = [str(channel) for channel in client.get_all_channels()]
if f'ticket-{ctx.author.id}' in channels:
await ctx.message.delete()
else:
ticket_channel = await ctx.guild.create_text_channel(f'ticket-{ctx.author.id}')
await ticket_channel.set_permissions(ctx.guild.default_role, send_messages=False, read_messages=False)
await ticket_channel.set_permissions(ctx.author, send_messages=True, read_messages=True, add_reactions=True, embed_links=True, attach_files=True, read_message_history=True, external_emojis=True)
embed = discord.Embed(color=10181046, description=f'Please enter the reason for this ticket, type `-close` if you want to close this ticket.')
embed.set_thumbnail(url='')
await ticket_channel.send(f'{ctx.author.mention}', embed=embed)
await ctx.message.delete()
logchannel = await client.fetch_channel('850364625479532545')
embed = discord.Embed(title="Ticket Created",
description="",
color=discord.Colour.green())
embed.add_field(name=f'**By:** {ctx.author}',value= f'**ID:** {ctx.author.id}')
await logchannel.send(embed=embed)
#--- Ticket Close ---
#client.command()
#commands.guild_only()
async def close(ctx):
print(f'{ctx.author} | {ctx.author.id} -> {client.command_prefix}close')
if ctx.channel.type != discord.ChannelType.private:
admin_roles = [834126146215477348,835608325273157733,838859643224326144,835582821221138472,835914273402126376]
if ctx.channel.name == f'ticket-{ctx.author.id}':
await ctx.channel.delete()
elif admin_roles and 'ticket' in ctx.channel.name or ctx.author.id in administrator_ids and 'ticket' in ctx.channel.name:
await ctx.channel.delete()
else:
await ctx.message.delete()
logchannel = await client.fetch_channel('850364625479532545')
embed = discord.Embed(title="Ticket Closed",
description="",
color=discord.Colour.red())
embed.add_field(name=f'**By:** {ctx.author}',value= f'**ID:** {ctx.author.id}')
await logchannel.send(embed=embed)
Tip: if you want to create a public bot don't use handwritten channel id, you'd better save to file for all servers in json file and set it with command.
For example this:
{
"server identifier": "system channel identifier",
...
}
I don't know what's wrong with the code.

On_member_join and on_member_leave not working discord.py

I've got on_member_joion and on_member_leave code, and it's not working, this is my code:
intents = discord.Intents.default()
intents.members = True
botprefix = ","
bot = commands.Bot(command_prefix = botprefix, case_insensitive=True, intents = intents)
#bot.event
async def on_mmember_join(member):
channel = bot.get_channel(803616331835899934)
await channel.send(f"Witaj {member.mention} na serwerze **Pogaduszki!**")
#bot.event
async def on_member_leave(member):
channel = bot.get_channel(803616331835899934)
await channel.send(f"Żegnamy {member.mention}, mamy nadzieję że do nas wrócisz")
And this code is not working, there are no errors, can anyone help please??
PS: I'm using https://replit.com
You have an error with your join event.
It should be on_member_join and not on_mmember_join.
Also, you should consider not using on_member_leave but on_member_remove.
Your full code:
#bot.event
async def on_member_join(member):
channel = bot.get_channel(803616331835899934)
await channel.send(f "Witaj {member.mention} na serwerze **Pogaduszki!**")
#bot.event
async def on_member_remove(member):
channel = bot.get_channel(803616331835899934)
await channel.send(f "Żegnamy {member.mention}, mamy nadzieję że do nas wrócisz")
Also have a look at the docs for more:
on_member_remove()

Discord py - Subcommand is not working properly

I'm trying to add a few subcommands to my code to make everything clearer.
Unfortunately, I have the problem that certain sections of code are simply not executed.
My Code:
#commands.group(invoke_without_command=True)
async def server(self, ctx):
created = ctx.guild.created_at
x = re.search("^.*:", str(created))
x = x.group()
x = x[:-6]
me = ctx.guild
embed = discord.Embed(title = f"Information about ``{ctx.guild}``", description=me.description, color = 0xf7fcfd, timestamp=ctx.message.created_at)
embed.add_field(name="__Information__", value=f"**Owner:** {me.owner}\n**Name:** {me.name}\n**ID:** {me.id}\n**Region:** {me.region}\n**Created at:** {x}", inline=False)
embed.add_field(name="__Server Information__", value=f"**Member**: {len(me.members)}\n**Roles:** {len(me.roles)}\n**Max Emojis:** {me.emoji_limit}\n**Emojis:** {len(me.emojis)}", inline=False)
embed.add_field(name="__Channel Information__", value=f"**Text-Channel:** {str(len(me.text_channels))}\n**Voice-Channel:** {str(len(me.voice_channels))}\n**AFK-Channel:** ``{me.afk_channel}``\n**AFK-Timeout:** ``{me.afk_timeout}sec``")
embed.set_footer(text=f"{ctx.message.author.name}", icon_url=ctx.message.author.avatar_url)
embed.set_thumbnail(url=me.icon_url)
embed.set_image(url=me.banner_url)
await ctx.send(embed=embed)
# SERVER AVATAR
#server.command()
async def avatar(self, ctx):
if not ctx.guild.icon:
embed = discord.Embed(title="Server has no avatar!", color=0xf7fcfd)
return await ctx.send(embed=embed)
else:
embed = discord.Embed(title=f"Avatar of {ctx.guild.name}", color=0xf7fcfd)
embed.set_image(url=ctx.guild.icon_url_as(size=1024))
await ctx.send(embed=embed)
# SERVER BANNER
#server.command()
async def banner(self, ctx):
if not ctx.guild.banner:
embed = discord.Embed(title="Server has no banner!", color=0xf7fcfd)
return await ctx.send(embed=embed)
else:
embed = discord.Embed(title=f"Banner of {ctx.guild.name}", color=0xf7fcfd)
embed.set_image(url=ctx.guild.banner_url_as(format='png'))
await ctx.send(embed=embed)
The problem is that the command ?server avatar does not provide any output. The commands ?server and ?server banner work perfectly.
Am I using the subcommands incorrectly or why do I get no output?
The reason why it didn't work is that I already had another command called avatar. The Bot would always take the command (avatar) instead the subcommand (server avatar).

How to make Discord.py get how MANY users reacted with a specific emoji?

I'm trying to make a poll command in Discord.py but want at the end the bot to send a list of users that reacted with 1️⃣ and another list of people that reacted with 2️⃣.
This is my code so far:
async def poll(ctx, q1, q2, time : int):
await ctx.send(f"React with 1 to vote for **{q1}** and with 2 to vote for **{q2}**\n**Poll Lasts for {time} seconds**")
poll = discord.Embed(title = "Poll", color = discord.Color.blue())
poll.add_field(name = f"{q1} 1️⃣", value = "᲼᲼᲼᲼᲼᲼")
poll.add_field(name = f"{q2} 2️⃣", value = "᲼᲼᲼᲼᲼᲼")
msg = await ctx.send(embed = poll)
r1 = await msg.add_reaction("1️⃣")
r2 = await msg.add_reaction("2️⃣")
await asyncio.sleep(time)
await ctx.send("Times up! **Poll Closed**")
new_msg = discord.utils.get(client.cached_messages,id = msg.id)
users1 = await r1.reactions[0].users().flatten()
users1.pop(users1.index(client.user))
users2 = await r2.reactions[0].users().flatten()
users2.pop(users2.index(client.user))
em=discord.Embed(title=f'Votes for {q1}', description=" , ".join(user.name for user in users1),color = discord.Colour.blue())
await ctx.send(embed = em)
em=discord.Embed(title=f'Votes for {q2}', description=" , ".join(user.name for user in users2),color = discord.Colour.blue())
await ctx.send(embed = em)
And this is the error I am getting:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'reactions'
Message.add_reaction returns None, you can't use these two lines
r1 = await msg.add_reaction("1️⃣")
r2 = await msg.add_reaction("2️⃣")
Easiest approach would be to fetch the message again with the updated reactions
message = await ctx.send("Whatever")
await message.add_reaction("1️⃣")
await message.add_reaction("2️⃣")
await asyncio.sleep(10)
updated_message = await ctx.channel.fetch_message(message.id)
users1, users2 = [], []
for r in updated_message.reactions:
print(f"{str(r)} was added {r.count} times")
if str(r) == "1️⃣":
users = await r.users().flatten()
users1.extend(users)
elif str(r) == "2️⃣":
users = await r.users().flatten()
users2.extend(users)
# `users1` and `users2` are lists of users that reacted with `1️⃣` and `2️⃣` accordingly
Reference:
Messageable.fetch_message
Message.reactions
Reaction.count
Reaction.users

expected an indented block error when making discord bot in python

I was making a discord bot in Python, and when i wanted to run it - the error occurred - expected an indented block, I'll show some code :
I tried to move some things but it didn't work. Also I'm completely new to Python and I have done some things with the tutorials on YouTube. Help me please.
import discord
import random
client = discord.Client()
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.idle, activity=discord.Game('?help'))
print('Bot jest online.')
#client.event
async def on_message(message):
id = client.get_guild(586513031765426198)
if message.content.find("?ping") != -1:
await message.channel.send("Pong! Jestem gotowy do użytku!")
if message.content.find("?plany") != -1:
await message.channel.send("Mam zamiar dodać do bota dużo komend!")
if message.content == "?rzut-monetą":
variable = [
'Orzeł!',
'Reszka!',]
await message.channel.send(random.choice(variable))
if message.content == "?pomoc":
embed = discord.Embed(title="Komendy!", description="Znajdziecie tutaj wszystkie komendy.", colour=discord.Colour.red())
embed.add_field(name="!ping", value="Sprawdza, czy bot jest teraz online.", inline=False)
embed.add_field(name="!info", value="Daje informacje o bocie.", inline=False)
embed.add_field(name="!plany", value="Pokazuje plany na przyszłość dotyczące bota.", inline=False)
embed.add_field(name="!pomoc", value="Pokazuje tą wiadomość.", inline=False)
embed.add_field(name="!rzut-monetą", value="Losuje pomiędzy orłem a reszką. Świetne do rozstrzygania sporów.", inline=False)
await message.channel.send(content=None, embed=embed)
if message.content == "?info":
embed = discord.Embed(title="Informacje!", description="Krótkie info o bocie.", colour=discord.Colour.blue())
embed.add_field(name="Autor", value="Pomi ;3#2203", inline=False)
embed.add_field(name="O bocie", value="Zadanie tego bota to głównie rozrywka, ale ma też wbudowanego auto-moderatora do polskich przekleństw.", inline=False)
await message.channel.send(content=None, embed=embed)
one line under the ?info command the first embed is showing on a red background. What could this mean?
You're missing an indented block on the last if
Where you have:
...
if message.content == "?info":
embed = discord.Embed(title="Informacje!", description="Krótkie info o bocie.", colour=discord.Colour.blue())
embed.add_field(name="Autor", value="Pomi ;3#2203", inline=False)
embed.add_field(name="O bocie", value="Zadanie tego bota to głównie rozrywka, ale ma też wbudowanego auto-moderatora do polskich przekleństw.", inline=False)
await message.channel.send(content=None, embed=embed)
Replace with:
...
if message.content == "?info":
embed = discord.Embed(title="Informacje!", description="Krótkie info o bocie.", colour=discord.Colour.blue())
embed.add_field(name="Autor", value="Pomi ;3#2203", inline=False)
embed.add_field(name="O bocie", value="Zadanie tego bota to głównie rozrywka, ale ma też wbudowanego auto-moderatora do polskich przekleństw.", inline=False)
await message.channel.send(content=None, embed=embed)
Note. If you intended to execute only part of that block, unindent whatever you don't care about the condition.

Categories

Resources