Delete channel on reaction // Ticket Bot discord.py - python

Im trying to make a ticket bot, and this is currently what i have. Now that i can make the bot create a channel after detecting a reaction, i need to be able to make it close the ticket on another reaction, as well as other features iw ant to add. But this reaction is in the channel created so i dont know how to get that channel id created. Someone suggested me to use cogs, but have no idea where to start.
async def on_raw_reaction_add(payload):
channel = payload.channel_id
if channel == 862711339298455563:
guildid = payload.guild_id
guild = client.get_guild(guildid)
channel = guild.get_channel(payload.channel_id)
message = channel.get_partial_message(payload.message_id)
emoji = '🎟️'
member = payload.member
payloaduserid = payload.user_id
clientuserid = client.user.id
if payloaduserid != clientuserid:
await message.remove_reaction(emoji, member)
category = client.get_channel(861003967000215583)
channel1 = await guild.create_text_channel(f'ticket {payload.user_id}', category = category )
ticketembed = discord.Embed(
title = f'Ticket - {payload.user_id}',
description = '- If you want to tag the admins please react with :telephone: \n - To report this message please react with :warning: \n - To close the ticket react this mesaage with :x: ',
color = discord.Color.red())
ticketembed.set_footer(text = 'All of the conversation will be held in the archives, eventhought a moderator can delete a message in this channel, a copy of it will be held in a location where only the owner can access.')
user = client.get_user(payload.user_id)
await channel1.set_permissions(target=user, read_messages=True , send_messages=True)
ticketembed1 = await channel1.send(embed= ticketembed)
await ticketembed1.add_reaction('☎️')
await ticketembed1.add_reaction('⚠️')
await ticketembed1.add_reaction('❌')
await channel1.send(f'{user.mention}', delete_after = 0.1) ```

One method is checking the channel name format. sometimes getting the channel from cache is not an option so we might need to fetch it.
async def on_raw_reaction_add(payload):
channel = client.get_channel(payload.channel_id)
# if channel is not in cache then fetch is using a API request
if not channel:
channel = await client.fetch_channel(channel_id)
# The user is the owner of the ticket since it is his id
if channel.name == f"ticket {payload.user_id}":
emoji = payload.emoji.name
# delete
if emoji == "❌":
await channel.delete()
It is better to save channel id and then use it since channel name can be changed. You can get the id like this.
channel1 = await guild.create_text_channel(f'ticket {payload.user_id}', category = category )
print(channel1.id)

Related

I want too add a !add command

Im trying to add !add command to my ticket bot using discord py.
the command would add a discord member to the channel and give them perms to send messages and see the channel history
Any help would be greatly appreciated
ive tried this
#commands.has_permissions(manage_channels=True)
async def add(ctx, member : discord.Member):
guild = ctx.message.guild
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True)
}
category = discord.utils.get(ctx.guild.channels, name="Alliances")
category = discord.utils.get(guild.categories, name = "enter Category")
channel = await guild.create_text_channel(
f'{ctx.author.name}', category = category)
await channel.set_permissions(guild.default_role, view_channel = False)
await channel.set_permissions(user, view_channel = True)
await channel.send(user.mention)
await channel.send("Ticket Opened !")
Something Like This

I created a channel with "guild.create_text_channel" but I couldn't find how to send a message to the channel I created

I created a channel with "guild.create_text_channel" but I couldn't find how to send a message to the channel I created. I'm using discord.py
async def on_raw_reaction_add(payload):
messageID = <myMessageID>
channell = bot.get_channel(payload.channel_id)
message = await channell.fetch_message(payload.message_id)
if messageID == payload.message_id:
member = payload.member
guild = member.guild
emoji = payload.emoji.name
overwrites = {
guild.default_role: discord.PermissionOverwrite(view_channel = False),
guild.me: discord.PermissionOverwrite(view_channel=True),
guild.get_role(<myRoleID>): discord.PermissionOverwrite(view_channel = True)
}
if emoji == '✅':
channel = guild.create_text_channel(str(member)+"-ticket", overwrites=overwrites,)
await channel
await message.remove_reaction('✅', member)
You can send a message in a channel using TextChannel.send.
PS. Guild.create_text_channel is a coroutine so you should await it.

Discord.py How can I create a channel with the nickname of user who clicked the emoji in message?

self.ticket_creator = user_id
message = await channel.fetch_message(message_id)
await message.remove_reaction("📩",user)
ticket_nr = random.randint(100,999)
self.channel_ticket = await category.create_text_channel(f'bilet-{ticket_nr}',
overwrites=overwrites)
Here, when a user clicks on the emoji, it deletes the emoji and creates a text channel whose title consists of random numbers.How can i make the discord nickname of user who clicked the emoji instead of these random numbers ?
Change self.channel_ticket = await category.create_text_channel(f'bilet-{ticket_nr}', overwrites=overwrites) to self.channel_ticket = await category.create_text_channel(user.name, overwrites=overwrites). This should do what you want.

Discord.py create a maximum of 1 channel per user

if message_id == 941751519781466172 and emoji == "📩":
self.ticket_creator = user_id
message = await channel.fetch_message(message_id)
await message.remove_reaction("📩",user)
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
support_role = guild.get_role(941751615998791740)
category = guild.get_channel(941749835328012328)
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
support_role: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
self.channel_ticket = await category.create_text_channel(f'övgü-{user.name}', overwrites=overwrites)
embed = discord.Embed(
title="Yetkililer en yakın zamanda yardımcı olmak için sizinle iletişime geçecek lütfen bekleyiniz.\n"
"Övgü satın alımı için ''Fiyatlar'' yazarak öğrenebilirsiniz.",
color=0xf1c40f)
msg = await self.channel_ticket.send(embed=embed)
dmmessage = (f'Övgü kanalınız oluşturuldu. (SAĞDAKİ YAZIYA TIKLAGİT)=====> <#{self.channel_ticket.id}> ')
dm = await user.send(dmmessage)
when the user clicks on the emoji in the message, it creates a private text channel for him and send a dm message that private channel has been created. a new channel is created every time the user clicks on it, how can I block it ?
if the user has created a private channel, I want him to send a dm message because you have already created a private channel.
nickname of the user becomes the name of the channel.
You can use an array to store the user id's on creation and only create a channel if the user id is not already in there. Like I mentioned you can also use a .json file or a database. The reason you would want to use one of these instead of a plain array is so that it is stored even if your script is turned off (say for maintenance purposes) the list of open channels will still be stored unlike in a plain array where it will get reset.
open_user_channels = []
if message_id == 941751519781466172 and emoji == "📩" and user_id not in open_user_channels:
self.ticket_creator = user_id
message = await channel.fetch_message(message_id)
await message.remove_reaction("📩",user)
member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
support_role = guild.get_role(941751615998791740)
category = guild.get_channel(941749835328012328)
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True),
support_role: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
self.channel_ticket = await category.create_text_channel(f'övgü-{user.name}', overwrites=overwrites)
embed = discord.Embed(
title="Yetkililer en yakın zamanda yardımcı olmak için sizinle iletişime geçecek lütfen bekleyiniz.\n"
"Övgü satın alımı için ''Fiyatlar'' yazarak öğrenebilirsiniz.",
color=0xf1c40f)
await self.channel_ticket.send(embed=embed)
dmmessage = (f'Övgü kanalınız oluşturuldu. (SAĞDAKİ YAZIYA TIKLAGİT)=====> <#{self.channel_ticket.id}> ')
await user.send(dmmessage)
open_user_channels.append(user_id)
else:
await user.send("There is an open channel already.")

How to get the sum and the names of all the users from all voice channels Disocrd?

I use :
import discord
I need to get from each voice channel amount all users and then get their names (usernames). How to do it?
You need to access the voice channel object. I recommend you use the voice channel's id. The command could look as follows:
#client.command(pass_context = True)
async def vcmembers(ctx, voice_channel_id):
#First getting the voice channel object
voice_channel = discord.utils.get(ctx.message.server.channels, id = voice_channel_id)
if not voice_channel:
return await client.say("That is not a valid voice channel.")
members = voice_channel.voice_members
member_names = '\n'.join([x.name for x in members])
embed = discord.Embed(title = "{} member(s) in {}".format(len(members), voice_channel.name),
description = member_names,
color=discord.Color.blue())
return await client.say(embed = embed)
And would work like this:
Where the number at the end is the channel id. If you don't know how to get the channel id, right click the channel and click Copy ID.
If you can't see the Copy ID, turn on Developer Mode in your Settings > Appearance > Developer Mode
You can also get all the members of a voice channel like this (updated for discord.py versions 1.0.0+):
#client.command(brief="returns a list of the people in the voice channels in the server",)
async def vcmembers(ctx):
#First getting the voice channels
voice_channel_list = ctx.guild.voice_channels
#getting the members in the voice channel
for voice_channels in voice_channel_list:
#list the members if there are any in the voice channel
if len(voice_channels.members) != 0:
if len(voice_channels.members) == 1:
await ctx.send("{} member in {}".format(len(voice_channels.members), voice_channels.name))
else:
await ctx.send("{} members in {}".format(len(voice_channels.members), voice_channels.name))
for members in voice_channels.members:
#if user does not have a nickname in the guild, send thier discord name. Otherwise, send thier guild nickname
if members.nick == None:
await ctx.send(members.name)
else:
await ctx.send(members.nick)

Categories