discord.py entering none instead of there #username - python

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)

Related

Is it possible to somehow define create_thread in PyCord

Hello I encountered the error 'Message' object has no attribute 'create_thread'
Code:
#commands.command()
async def sugesstion(self, ctx, *,sugestia):
await ctx.message.delete()
embed = discord.Embed(colour = 0xFFA500)
embed.set_author(name=f' {ctx.message.author}', icon_url = f'{ctx.author.avatar_url}')
embed.add_field(name = 'Member,', value = f'{sugestia}')
embed.set_footer(text="Apples")
msg = await ctx.send(embed=embed)
await msg.add_reaction('✔️')
await msg.add_reaction('➖')
await msg.create_thread(name="Comments", auto_archive_duration=MAX)

How to create a new channel using nextcord

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.

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')

How to unban a discord user given their ID

The code I'm currently using was perfectly fine, but I really want to unban with ID because it is much easier for me.
Current command is in a Cog file:
import discord
from discord.ext import commands
class unban(commands.Cog):
def __init__(self, client):
self.client = client
#commands.command()
#commands.has_permissions(administrator=True)
async def unban(self, ctx, *, member : discord.Member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split("#")
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
unban = discord.Embed(title='UnBan Hammer Has Spoken! :boom:', description=f'**Moderator:** {ctx.author}\n **User UnBanned:** {member}\n', color=0x10940b)
unban.set_author(name="Moderating Action", icon_url=ctx.author.avatar_url)
await ctx.send(embed=unban)
return
def setup(client):
client.add_cog(unban(client))
How can I update this code to unban using the user ID instead?
async def unban(self, ctx, id: int) :
user = await client.fetch_user(id)
await ctx.guild.unban(user)
await ctx.send(f'{user} has been unbanned')
this should work for you, you can personalize even more, but that's just the main code you need.
Try
async def unban(self, ctx, *, member : discord.User):
await ctx.guild.unban(discord.Object(id = member.id))
unban = discord.Embed(title='UnBan Hammer Has Spoken! :boom:', description=f'**Moderator:** {ctx.author}\n **User UnBanned:** {member}\n', color=0x10940b)
unban.set_author(name="Moderating Action", icon_url=ctx.author.avatar_url)
await ctx.send(embed=unban)

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!

Categories

Resources