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.
Related
I'm making a suggestion discord bot, and I'd like to know if there's how I can add an image that the user posted on embed
something like
embed.set_image(url=image_url)
the image_url would be the image the user added to the message.
suggestion_channel_id = 925568558241550393
color = 0x3498db
#bot.event
async def on_message(message):
suggestionData = '{}'.format(message.content).replace('?sugestão','')
if message.author == client.user:
return
if message.content.startswith('?sugestão'):
embed=discord.Embed(title ='Nova sugestão!')
embed.add_field(name='Vote Abaixo!', value=suggestionData, inline=True)
channel=bot.get_channel(suggestion_channel_id)
embed.color=color
embed.set_footer(text='Sugestão por ' + message.author.name, icon_url=message.author.avatar_url)
up_emoji = '\N{THUMBS UP SIGN}'
down_emoji = '\N{THUMBS DOWN SIGN}'
msg = await channel.send(embed=embed)
await msg.add_reaction(up_emoji)
await msg.add_reaction(down_emoji)
await message.channel.send('Sua sugestão foi enviada!' + '** ' + message.author.name + ' 😎' + ' **')
token = config("token")
bot.run(token)
You can use message.attachments to get a list of message attachments:
#bot.command()
async def send_image(ctx):
embed = discord.Embed(title="Your attached image")
if len(message.attachments):
embed.set_image(url=ctx.message.attachments[0].url)
await ctx.send(embed=embed)
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).
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
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)
I have got an error: TypeError: on_message() missing 1 required positional argument: 'member'
How do I fix this error and what do I have to do with it?
Here is my code:
import discord
import config
client = discord.Client()
#client.event
async def on_message(message, member):
id = client.get_guild(config.ID) # ID в файле config
channels = [
647074685535649802,
636901028478058497,
690272147050070158,
694196995887202375,
690276595578962177,
654662320735387648,
650381379892412426,
641704849196711976,
]
badwords = ["лузер", "расизм", "нацизм"]
valid_users = ["Resadesker#1103"]
unwarnusers = ["ResadeskerBOT#7104"]
if str(message.author) in valid_users:
for channelo in channels:
if message.content[:message.content.find(' ')] == "$spam":
channel = client.get_channel(channelo)
await channel.send(message.content[message.content.find(' '):])
for word in badwords:
if word in message.content.lower():
if str(message.author) != "ResadeskerBOT#7104":
warnFile = open("D:/python/disbot/warns.txt", "a")
warnFile.write(str(message.author) + "\n")
warnFile.close()
mutedRole = discord.utils.get(message.guild.roles, name='JB-MUTED')
await member.add_roles(mutedRole)
channel = client.get_channel(696315924591935488)
await channel.send(f"--------------------\nЗа человеком {message.author.mention} было замечено нарушение. \nВот его сообщение: \n{message.content} \nНарушение было в канале {message.channel}\n--------------------")
client.run(config.TOKEN)
You are using old code from before the discord.py rewrite but running a newer version.
Just change your:
#client.event
async def on_message(message, member):
to
#client.event
async def on_message(message):
And it should work.