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
Related
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.
components = [
[
Button(label="Open a ticket", style=2, custom_id="Open", emoji="📩")
]
]
await ctx.send(embed=embed, components=components )
while True:
interaction = await client.wait_for("button_click", check = lambda i: i.component.label.startswith("Open"))
overwrites = {
member_role: discord.PermissionOverwrite(view_channel=False),
ctx.author: discord.PermissionOverwrite(view_channel=True),
guild.me: discord.PermissionOverwrite(view_channel=True),
ticket_mod_role: discord.PermissionOverwrite(view_channel=True)
}
Embed = discord.Embed(
title="Ticket Created",
description=f"{THIS EMPTY SPACE} Your ticket is created",
)
await interaction.send(content = await interaction.send(embed=Embed), ephemeral=True)
ticket = await ticket_category.create_text_channel(
f"🎫│Ticket-{THIS EMPTY SPACE}", overwrites=overwrites
)
Embed = discord.Embed(
title="Ticket Created",
url="",
description=f"""{ctx.author.name} Wait our staff.""",
color=discord.Color.random()
)
Instead of ctx.author.name (ctx.author is a discord.member class)
Use ctx.author.mention
Docs:
https://discordpy.readthedocs.io/en/stable/api.html?highlight=mention#discord.Member.mention
i've resolved using the library of discord.py (interaction) instead of this i used the object (user.name)
import discord
from discord.ext import commands
from discord_buttons_plugin import *
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='>', intents=intents)
buttons = ButtonsClient(client)
#client.event
async def on_ready():
print("Log : "+str(client.user))
#client.command()
#commands.has_permissions(administrator=True)
async def ticket(ctx):
em = discord.Embed(title="join us",description="To create a ticket react with",emoji={"id": None,"name": "📩","animated": False},color=discord.Color.from_rgb(0,255,0))
await buttons.send(embed=em,channel=ctx.channel.id,
components=[ActionRow([Button(style= ButtonType().Secondary,label = "Create ticket",custom_id = "test1")])])
#buttons.click
async def test1(ctx):
guild = ctx.guild
ch = await guild.create_text_channel(name=f'ticket-{ctx.member}')
await ch.set_permissions(ctx.guild.default_role,view_channel=False,send_messages=False)
await ch.set_permissions(ctx.member,view_channel=True,send_messages=True)
await ctx.reply(f"Ticket created {ch.mention}",flags=MessageFlags().EPHEMERAL)
client.run("token")
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.")
So im making this Discord bot and one of the commands is to add api names and what comes with the api and so that command works and the code looks like this:
#Add a Method to the json string command
#bot.command()
#commands.has_role('Reseller')
async def addmethod(ctx,api, method):
methods = await get_method_data()
methods[str(api)] = {}
methods[str(api)]['Method'] = method
methods[str(api)]['api'] = api
with open("methods.json", "w") as output:
json.dump(methods,output)
await ctx.channel.purge(limit=1)
embed = discord.Embed(title=f'{method} has been added to list', color=0x000000)
await ctx.send(embed=embed)
Then im making another command where it grabs all the data from the json file and prints it into the Discord chat but its not making the data that i inputted print into the Discord chat here is the code of the ERROR
#bot.command()
async def methodss(ctx):
methods = await get_method_data()
method = methods['Method']
apiname = methods['api']
em = discord.Embed(title = "B")
em.add_field(name = "Wallet Balance", value = method)
em.add_field(name = "Bank balance", value = apiname)
await ctx.send(embed = em)
Where did I go wrong??
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)