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).
Related
I would appreciate if anyone out there could help me.
I wanted to code a bot for a esports team, which does have a ticket included, on pycharm on my computer actually everything works fine, but when i then want to host it on replit it doesnt work. I have made the ticket with discord-components and as the button is clicked, the bot thinks, but does never send the messege and goes on with the code. But on my other replit file I have another ticket, but that does work just fine.
Does someone have an idea what could have gone wrong?
#client.command()
async def tryout(ctx):
server = client.get_guild(879321194506121247)
tryouter = get(server.roles, name="Tryouter")
embed_tryout = discord.Embed(title="Tryout",
description="*Start your tryout right now, to join HOC-Esports and start your clash royale esports journey*",
color=discord.Color.from_rgb(244, 246, 246))
embed_tryout.add_field(name="What we offer:",
value="• 4-5 scrims per week\n• A good organised team with engaged managers\n• Custom HOC logo + banner",
inline=False)
embed_tryout.add_field(name="What we demand from you:",
value="• You are engaged to improve yourself\n• You aren't toxic",
inline=False)
embed_tryout.add_field(name="Informations to the tryout""Infos zum Tryout",
value=f"You will make a bo5 against a {tryouter.mention}, who will rate your skill decide if you fit in our team or not",
inline=False)
await ctx.send(embed=embed_tryout, components=[
[Button(label="🎫 start tryout", style=1, custom_id="button1")]
])
#client.event
async def on_button_click(interaction):
if interaction.custom_id == "button1":
pass
else:
return
guild = interaction.guild
tryouter = get(guild.roles, name="Tryouter")
category = client.get_channel(997078909071929434)
await interaction.send(content="You just started a tryout", ephemeral=True)
benutzer = str(interaction.user)
user_letters = len(benutzer)
username = benutzer[:user_letters - 4]
ticket_channel = await guild.create_text_channel(name=f"Tryout of {username}", category=category)
user = interaction.author
tryoutrole = get(guild.roles, name= "Tryout")
visitorrole = get(guild.roles, name="Besucher")
esports_role = get(guild.roles, id=917100410400014426)
await user.add_roles(tryoutrole)
await user.remove_roles(besucherrolle)
await ticket_channel.set_permissions(user, view_channel=True, read_message_history=True, read_messages=True,
add_reactions=True, embed_links=True, attach_files=True,
use_external_emojis=True)
await ticket_channel.send(
f"Welcome {interaction.user.mention}. You just started a tryout.\nA {tryouter.mention} will take care of you.\nPlease send meanwhile your friendlink and your profile right here\nIf you don't want to make the tryout anymore, just close the ticket.",
components=[
Button(label="🔒 close ticket", style=2, custom_id="button2")
])
interaction2 = await client.wait_for("button_click", check=lambda i: i.custom_id == "button2")
await interaction2.send(content="You just closed the ticket", ephemeral=True)
time.sleep(5)
await ticket_channel.delete()
await user.remove_roles(tryoutrole)
if esports_role in user.roles:
return
await user.add_roles(visiterrole)
my code does not work. Everytime I try to fix it it does not work. This is what I have finished at and it says that the command has not been found. Anyone got a solution?
#bot.command()
async def suggest(self, ctx, *,suggestion):
await ctx.channel.purge(limit = 1)
channel = discord.utils.get(ctx.guild.text_channels, name = '💡suggestions')
suggestEmbed = discord.Embed(colour = 0xFF0000)
suggestEmbed.set_author(name=f'Suggested by {ctx.message.author}', icon_url = f'{ctx.author.avatar_url}')
suggestEmbed.add_field(name = 'Suggestion', value = f'{suggestion}')
message = await ctx.send(embed=suggestEmbed)
await message.add_reaction('✅')
await message.add_reaction('❌')
First of all, a better way to delete command usage is await ctx.message.delete()
Second, you should use client.get_guild() and client.get_channels() instead of channel = discord.utils.get(ctx.guild.text_channels, name = '💡suggestions')
Also, if you could include any errors, it would be helpful.
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.
I know how to send embeds in discord.py but I am a little bit confused about this code
Please, anyone, help me with a sample of code for this command.
#client.command()
async def wanted(ctx, user: discord.Member = None):
if user == None:
user = ctx.author
wanted = Image.open("wanted.jpg")
asset = user.avatar_url_as(size = 128)
data = BytesIO(await asset.read())
pfp = Image.open(data)
pfp = pfp.resize((257, 257))
wanted.paste(pfp, (99, 201))
wanted.save("profile.jpg")
await ctx.send(file = discord.File("profile.jpg"))
To send a local image in an embed:
file = discord.File("some_file_path", filename="image.png")
embed = discord.Embed()
embed.set_image(url="attachment://image.png") # Same name as in the file constructor
await ctx.send(file=file, embed=embed)
To add the pfp of the invoker to an embed
embed = discord.Embed()
embed.set_image(url=ctx.author.avatar_url)
await ctx.send(embed=embed)
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)