discord.py sending message to channel name instead of ID - discord

I am a beginner to this programming stuff, and I have a quick question. I am trying to make a logs channel for multiple servers, and I want to be able to look for a channel that has the word “logs” in it, so can you help me?
Code:
#client.event
async def on_command_completion(ctx):
channel = client.get_channel('829067962316750898')
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
await channel.send(embed=embed)

You need to get the channel. I suggest using discord.utils.get.
Also, try using {ctx.channel.mention} to mention the channel instead.
#client.event
async def on_command_completion(ctx):
channel = discord.utils.get(ctx.guild.text_channels, name='logs')
if channel is None:
return # If there is no logs channel, do nothing (or what you want to do)
embed = discord.Embed(colour=discord.Color.green(),
title="Command Executed")
embed.add_field(name="Command:", value=f"`,{ctx.command}`")
embed.add_field(name="User:", value=f"{ctx.author.mention}", inline=False)
embed.add_field(name="Channel:",
value=f"{channel.name} **( {channel.mention} )**")
await channel.send(embed=embed)
See the discord.TextChannel docs and discord.utils.get()

Related

get the number of users in a voice channel with my discord bot in python

I'm looking for a way to get the number of users in a voice channel and to get their username to use it in one of my discord bot command, I found that I can use ctx.guild.member to get the members of users in the server, but not in a specific channel, is there a method to do that ?
I tried this
from discord.ext import commands
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.author.VoiceChannel
Or this
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
None of them works, the first tells me that member objects has no attribute VoiceChannel
and the second one tells me that ctx.message.author.voice is a NoneType object and has no attribute channel
You're very close.
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
ctx has an author property; this returns the user/member object for the individual that invoked the command. The user/member has a voice property that returns the VoiceState object of the user. This has a channel property which will return the voice channel that the user is in. Importantly, this will be None if the user isn't in a voice channel. So, putting it together:
#bot.command()
async def teamup(ctx):
voice = ctx.author.voice
if not voice.channel:
await ctx.send("You are not currently in a voice channel")
return
channel = voice.channel
# do what you want with the channel here
await ctx.send(f"You're in {channel.name}!")

How can I have a trigger without my command prefix, like welcome and not $welcome?

So I wanted to make a feature on my discord bot and I wanted it to respond when users say welcome and say Welcome! back. But every time I try it only responds to $welcome. ($ is my prefix). What should I do?
client = commands.Bot(command_prefix = "$")
#client.command()
async def welcome(ctx, message):
if any(word in message for word in welcome):
await message.channel.send("Welcome!")
#client.event
async def on_message(message):
if "welcome" in message.content:
await message.channel.send('Welcome!')

Discord.py doesn't update on member remove

I am making a discord bot that changes a voice channels' name when a user joins or leaves to the amount of members on the server. My issue is that when a user leaves, it doesn't update. Any help is appreciated.
#bot.event
async def on_raw_member_remove(member):
channel = discord.utils.get(member.guild.channels, id=973603264639668248)
await channel.edit(name=f'Member Count: {member.guild.member_count}')
The right event for this would be on_member_remove.
You can also get the channel in a much easier way and edit it.
See a possible new code:
#bot.event
async def on_member_remove(member: discord.Member):
channel = bot.get_channel(Channel_ID_here)
await channel.edit(name=f"Member Count: {len(member.guild.members)}")
The reason it doesn't work is because you are using on_raw_member_remove
You should be using on_member_leave as the member is not getting removed.

Welcome message Discord.py

I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)

Discord.py welcome bot on_member_join event not getting callded

I've been interested in working with discord bots lately, and from what I'm seeing this code should work but it is not...
I'm simply just playing around with the API because it's fun so I'm pretty new with this. I just want the bot to welcome someone when they join.
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
channel = client.guilds[0].get_channel(CHANNEL ID)
await channel.send("Bot online")
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
#client.event
async def on_member_join(member):
print(f'{member.name} has joined the server')
channel = client.guilds[0].get_channel(CHANNEL ID)
print(channel)
await channel.send(f'{member.name} has joined the server')
#client.event
async def on_member_remove(member):
print(f'{member.name} has left the server')
channel = client.guilds[0].get_channel(CHANNEL ID)
print(channel)
await channel.send(f'{member.name} has left the server')
client.run('TOKEN HERE')
instead of member.name use member and if that dosnt work add this where you have your variables intents = discord.Intents(messages = True, guilds = True, reactions = True, members = True, presences = True)
client = commands.Bot(command_prefix = '*', intents = intents)
FOR discord.py >= 1.5
1.5 adds support for Gateway Intents, by default your bot doesn't have access to guild members like it did in previous versions. If your bot is in less than 100 servers then you can enable these intents without verification. You should see these settings at the bottom of your Bot page on the Discord Developer Portal. If you enable both, then you need to change your Client (or commands.Bot) instantiation as such:
intents = discord.Intents.all()
client = discord.Client(intents=intents)
When I test this, the event is firing, but I'll bet your issue lies in the line:
channel = client.guilds[0].get_channel(CHANNEL ID)
It will be far more reliable to just use use client.get_channel to ensure you are actually grabbing the intended channel, all channel IDs on discord are unique so you do not need to use a guild object. Also CHANNEL ID will not be a valid variable, but I am guessing you just redacted it.
You forgot the brackets for the decorators.
#client.event
should be
#client.event()
EDIT: Not it, apparently. Will update this answer when OP replies with more information.

Resources