I am using a #client.event function for my on_member_join event. I would like it to send a message upon a user joining, however, there are no responses nor errors from the console.
Here is my current attempted code
#client.event
async def on_member_join(member):
await member.send(
"<a:welcome1:805857483473027122><a:welcome2:805857477375164468> \n<a:tysm:805858522578812930>"
)
Events related to members or on_member_join event, will require the special member intents to be enabled in the developer portal as well as defined in your code.
After enabling the intents in the bot section of the portal, copy this into your code to use the special member_intents module
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='Prefix', intents=intents)
Related
My bot has fully functioning /slash commands and I would also like them to be text commands and add other functionality when users send messages in chat. However, I cannot seem to get the bot to listen for messages.
import interactions
# Create the bot
Bot = interactions.Client(
token = BotToken,
scope = ServerID
)
#Bot.event
async def on_message(message):
await HandleMessage(message)
return
Bot.start()
The code never enters the on_message method.
I had a working version with the discord.py module and they are very similar. I am not sure why it does not work with Interactions.py.
Working code with discord.py:
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
client = discord.Client(intents = intents)
#client.event
async def on_message(message):
await HandleMessage(message)
client.run(Token)
i'm following the RealPython tutorial on how to make a discord bot and have come across the issue where my code only prints the name of the bot, but not mine. i've seen solutions where you enable priveleged gateway intents in the developer portal and add the discord.Client(intents=intents) code to the beginning of your code, so i did, but the problem still persists. i'm not getting any error or anything, just that only my bot's name prints.
please let me know if i missed out anything or if there's another issue i need to solve. thanks!
here's my code:
# bot.py
import os
import discord
from dotenv import load_dotenv
intents = discord.Intents.all()
client = discord.Client(intents=intents)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
client = discord.Client()
#client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
break
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
client.run(TOKEN)
You have two lines where you assign a discord.Client to client.
client = discord.Client(intents=intents)
# and
client = discord.Client()
The first line is done properly with the intents. The second line, however, makes a new discord.Client object with no intents. Remove one of these lines and make sure intents are enabled on the line you keep.
I am trying to get a server object by the server ID. I know the server_id is correct, but for some reason, the functions return me none
client = discord.Client()
TOKEN = "mytoken"
client.login(TOKEN)
server_id=846557514476945408 #this is my own server id
guild=client.get_guild(server_id)
I've checked made sure that my client connection is open.
Not sure if it matters, but I am seeing these warnings
<input>:1: RuntimeWarning: coroutine 'Client.connect' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
The server_id and guild variables run before the client is ready so you always receive none. It also seems that the code isn't being set up correctly since client.login() is a coroutine and is never awaited. Below I removed the client.login() and instead used client.run() and used the minimal bot example from discord.py to make use of the on_ready event included. Discord.py - A Minimal Bot
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
server_id = 846557514476945408 #this is my own server id
guild = client.get_guild(server_id)
client.run("TOKEN")
So basically, my server is about to hit 100 members, and I wanna make a #client.event (if not event then command) where it detects when 100 members is hit, and if it is, it announces it in my announcement channel. I've searched everywhere for it but i've never gotten a result! Does anyone know?
You would really only need to check if the guild has 100 members if a new member joins (or leaves, but that would be a bit depressing). For that, you can use the on_member_join() event reference.
Make sure that member intents are enabled, both in the Discord Developer Portal (in the section called Bot, under Privileged Gateway Intents), as well as in your script.
import discord
from discord.ext import commands
# Enable member intents
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
CHANNEL_ID = 1234567890
#bot.event
async def on_member_join(member):
channel = await bot.fetch_channel(CHANNEL_ID)
if member.guild.member_count == 100:
await channel.send("100 Members! :partying_face:")
CHANNEL_ID would be the ID of the channel to send the celebratory message in.
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.