How to create a new channel using nextcord - python

I believe it goes something like this
#bot.command()
async def ticket(ctx, member, name):
await create_text_channel(name = f'{member}', subject = "test channel")
Please correct me if I am wrong

async def ticket(ctx, channelName):
guild = ctx.guild
embedTicket = nextcord.Embed(title = "Ticket", description="Sehr gut!")
if ctx.author.guild_permissions.manage_channels:
await guild.create_text_channel(name=f'{channelName}-ticket')
await ctx.send(embed=embedTicket)
This would be a way to do it.

Related

discord.py trying to set up a moderation log channel

So I'm trying to set up a log channel where it will post a message in that whenever someone gets kicked. At the moment I'm storing it in a json file, but i cant get it tow work. Does anyone have the code? Here's mine:
with open('log_channels.json', 'r') as f:
log_channels = json.load(f)
#client.command()
#commands.has_permissions(administrator = True)
async def modlog(ctx, channel = None):
log_channels[str(ctx.guild.id)] = channel.split()
with open('log_channels.json', 'w') as f:
json.dump(log_channels, f)
await ctx.send(f'Mod log set to {channel}')
#client.command()
#commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
await member.kick(reason = reason)
await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))
guild = ctx.guild
log_channels.get((guild.id) ('0'))
print (log_channels)
channel = client.get_channel(log_channels)
await channel.send(f"test")
Your error comes from this line:
log_channels.get((guild.id) ('0'))
And more precisely, from (guild.id) ('0'), which is equivalent to 123456789012345678('0') and is impossible.
What you probably meant to do is this:
#client.command()
#commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
await member.kick(reason = reason)
await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))
channel_id = log_channels.get(str(ctx.guild.id), '0')
channel = client.get_channel(channel_id)
await channel.send(f"test")
NB: json dict keys must be strings, so you need to get str(ctx.guild.id).
Also, if log_channel equals '0', channel will be None and channel.send() will raise an error.
What I would recommend doing is this:
#client.command()
#commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason = None):
await member.kick(reason = reason)
await ctx.send(embed=discord.Embed(title=f'{member} has been kicked.', color= 0xFF8633))
if str(ctx.guild.id) in logs_channels:
channel = client.get_channel(log_channels[str(ctx.guild.id)])
await channel.send(f"test")
else:
# Not mandatory, you can remove the else statement if you want
print('No log channel')

discord.py entering none instead of there #username

Here is what i have right now!
#client.command()
async def punch(ctx, *, response):
response = response.replace("(", "")
response = response.replace(")", "")
member = discord.utils.get(client.get_all_members(), id=response)
embed = discord.Embed(title=f":house_with_garden: Punched: {member} :punch:", color=discord.Colour.green())
await ctx.send(embed=embed)
And this is what happens: I want it to be Sauq#(then my tag)
You can use MemberConverter
#client.command()
async def punch(ctx, member: discord.Member): # By typehinting the member arg, `MemberConverter` will automatically convert it to a `discord.Member` instance
embed = discord.Embed(title=f":house_with_garden: Punched {member} :punch:", color=discord.Colour.green()) # If you want to mention the member: `member.mention`
await ctx.send(embed=embed)

How can I edit embed with emoji buttons in discord.py?

How can I edit embed with emoji buttons in discord.py?
I was making it like this.. but it doesn't works..
#client.event
async def on_message(message):
if message.content.startswith("!menu"):
embed=discord.Embed(color=0x00FFFF, title=f'Menu', description= f'πŸ”΄ - test1 \n🟠 - test2 \n🟑 - test3 \n🟒 - test4 \nπŸ”΅ - test5 \n🟣 - test6', timestamp=message.created_at)
embed.set_footer(text=f'-', icon_url=message.author.avatar_url)
msg = await message.channel.send(embed=embed)
await msg.add_reaction('πŸ”΄')await msg.reaction_add('🟠')
await msg.add_reaction('🟑')
await msg.add_reaction('🟒')
await msg.add_reaction('πŸ”΅')
await msg.add_reaction('🟣')
if str(msg.add_reaction) == 'πŸ”΄':
embed1=discord.Embed(color=0x00FFFF, title=f'edit1', description= f'test1')
await msg.edit(embed=embed1)
I want some edit codes!!
Welcome to StackOverflow, you can use discord.ext.menus. Here's how you need to install it.
pip install git+https://github.com/Rapptz/discord-ext-menus.
Then you can do something like this.
import discord
from discord.ext import menus
class ColorMenu(menus.Menu):
def __init__(self, embed):
super().__init__(timeout=30.0, delete_message_after=True)
self.embed = embed
self.result = None
async def send_initial_message(self, ctx, channel):
return await channel.send(embed=self.embed)
#menus.button('πŸ”΄')
async def red_button(self, payload):
# do what ever you want with this.
# right now I'm going to just edit the message
self.embed.description = 'test1'
self.embed.title = 'title1'
self.message.edit(embed=self.embed)
# do the same thing for other reactions
Then make a new command and instantiate the class, like this:
#bot.command()
async def menu(ctx):
color_menu = ColorMenu()
color_menu.start(ctx)
And you're done.
Good luck with your bot.

Suggestion bot discord.py

I want the bot to react to its own message with the βœ… and ❎ emojis for a suggestion command. Here is the code. How do i do it?
import discord
from discord.ext import commands
class suggestions(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command(description = 'Add a suggestion for this community!')
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 = 'New suggestion!', value = f'{suggestion}')
await channel.send(embed=suggestEmbed)
def setup(bot):
bot.add_cog(suggestions(bot))
Messageable.send returns a discord.Message object, you can then simply .add_reaction
message = await ctx.send(embed=suggestEmbed)
await message.add_reaction('βœ…')
await message.add_reaction('❌')
Note: You need the unicode of the emoji to react, to get it simply \:{emoji}:
Reference:
Messageable.send
Message.add_reaction
Me, I use this:
#bot.command()
async def suggestion(ctx, *, content: str):
title, description= content.split('/')
embed = discord.Embed(title=title, description=description, color=0x00ff40)
channel = bot.get_channel(insert the channel ID here)
vote = await channel.send(embed=embed)
await vote.add_reaction("βœ…")
await vote.add_reaction("❌")
await ctx.send("your suggestion has been send")
You can vote using the emojis, enjoy!

Trying to send a message to a specific channel using Discord.py rewrite and it isn't working

I'm currently working on a discord bot and I'm trying to send a message to a specific channel using Discord.py rewrite once the user levels up, and I'm getting this error:
await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
AttributeError: 'NoneType' object has no attribute 'message'
Here is all the code:
import discord
from discord.ext import commands
import json
import asyncio
class Levels(commands.Cog):
#commands.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
return
author_id = str(message.author.id)
bot = commands.Bot(command_prefix='!')
if author_id not in self.users:
self.users[author_id] = {}
self.users[author_id]['level'] = 1
self.users[author_id]['exp'] = 0
self.users[author_id]['exp'] += 1
if author_id in self.users:
if self.lvl_up(author_id):
channel = bot.get_channel('636399538650742795')
await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
def __init__(self, bot):
self.bot = bot
with open(r"cogs\userdata.json", 'r') as f:
self.users = json.load(f)
self.bot.loop.create_task(self.save_users())
async def save_users(self):
await self.bot.wait_until_ready()
while not self.bot.is_closed():
with open(r"cogs\userdata.json", 'w') as f:
json.dump(self.users, f, indent=4)
await asyncio.sleep(5)
def lvl_up(self, author_id):
author_id = str(author_id)
current_xp = self.users[author_id]['exp']
current_lvl = self.users[author_id]['level']
if current_xp >= ((3 * (current_lvl ** 2)) / .5):
self.users[author_id]['level'] += 1
return True
else:
return False
I'm really not sure what the issue is here but if anyone knows the problem I would be very appreciative if you could let me know how I can correct this.
Thanks for reading I've been trying to figure this out for hours.
Edit: Still having the issue.
You get AttributeError because channel is None.
To fix it you need to remove quotes from channel id like this:
channel = bot.get_channel(636399538650742795)
This is described here: https://discordpy.readthedocs.io/en/latest/migrating.html#snowflakes-are-int
Also i see another error on the next line. The channel has no message attribute too. I think you need to fix it like this:
await channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
I was able to send messages using this guide: https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-send-a-message-to-a-specific-channel
the code I used is:
channel = client.get_channel(12324234183172)
await channel.send('hello')
#bot.command()
async def lvl_up(member: discord.Member):
"""Send a level up message"""
channel = bot.get_channel(channel_id) # channel id should be an int
if not channel:
return
await channel.send(f"GG {member}, u lvled up") # Whatever msg u want to put
Try using that code for the channel and sending the message, then add ur logic. I'm new to Stack Overflow so idk if I formatted that code correctly
Not sure if this is solved (sorry I'm new to stack overflow) but using this made it work
#bot.command()
async def Hello(ctx):
channel = bot.get_channel(Insert Channel ID)
await channel.send('Hello')
Using this I didn't get the NoneType error.
you need to put it in an async function
so instead of
channel = bot.get_channel(<channel id>)
you should do
async def get_channel():
channel = bot.get_channel(<channel id>)
asyncio.run(get_channel())

Categories

Resources