Discord Py Bot users Status - python

#client.event
async def on_ready():
await client.wait_until_ready()
await client.change_presence(activity=Activity(name=f".help auf {len(client.guilds)} Servers",
type=ActivityType.playing))
I want to show the status of the total amount of members. How can I do this?

Axiumin's answer is correct but that requires you to have the intent.members enabled.
An example of enabling it can be done like so
intents = discord.Intents()
intents.all()
client = commands.Bot(command_prefix=".", intents=intents)
#client.event
async def on_ready():
await client.wait_until_ready()
await client.change_presence(activity=Activity(name=f".help auf {len(client.users)} Users", type=ActivityType.playing))
You will have to enable it here. Select the application you want it on -> Select Bot -> SERVER MEMBERS INTENT and then make sure it shows blue next to it. Then click Save Changes. Since you are developing your bot you might want to enable Presence intent as well to save you time later.
However, if your bot is not allowed it(if your bot is in 100+ servers and discord rejected it's request) you can get around this by doing:
#client.event
async def on_ready():
await client.wait_until_ready()
total_members = 0
for guild in client.guilds:
total_members += guild.member_count
await client.change_presence(activity=Activity(name=f".help auf {total_members} members", type=ActivityType.playing))

NOTE: This counts the total amount of users in all servers the bot is in, not how many are using the bot at that time. In order to do that, you'd have to use a counter and increment it each time a new user uses the bot, which I won't go into unless that's what you're trying to do.
This should work: len(client.users). You'd use it in your change_presence code like this:
await client.change_presence(activity=Activity(name=f".help auf {len(client.users)} Users", type=ActivityType.playing))

Related

Discord Bot won't reply to messages in channel [duplicate]

This question already has an answer here:
Commands don't run in discord.py 2.0 - no errors, but run in discord.py 1.7.3
(1 answer)
Closed 5 months ago.
I'm not sure what is wrong with my python code, but I've been trying to get this to work wit ha bunch of different variations for a couple days now. The bot launches and goes online, but whenever I try to input a command into discord, the bot never responds. I've tried in multiple servers as well as my own. Here is my code:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send("Hello!")
client.run("original token would be here")
From your code (if it's complete), it seems like you're missing a necessary part, which are your intents.
Did you declare them somewhere in the code?
You also have to set the permissions you want the bot to have when joining the server, under discord.com/developers/applications/
In version 1.5 comes the introduction of Intents. This is a radical change in how bots are written. An intent basically allows a bot to subscribe to specific buckets of events.
Source: Discordpy ReadTheDocs
import discord
intent = discord.Intents.all()
client = discord.Client(intents = intent)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
#client.event
async def on_message(message):
if message.author.name == client.user: return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('TOKEN GOES HERE')
Additionally, you might want to check the documentation for discord.ext in order to implement #bot.commands and avoid hard coding the command followed by message conditionals.

Discord.py bot doesn't dm new members

I'm using the event on_member_join to attempt to dm new members, but when I tested with my alt, it didn't send a message.
#bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
channel = bot.get_channel(803703488499810326)
userDM = member.create_dm()
await userDM.send(embed=embed)
You don't need to do member.create_dm() because when you DM a user it's not needed.
You also don't need to define description to None, you simply don't even need to specify a description.
In order for the bot to even find the members, you need members intent turned on in your bot. You can do that using the image below.
Now, you just need to provide the code in the bot so that the bot can use the intents.
Here is the code that you need to add to your main bot file.
intents = discord.Intents.default() # Gets the default intents from discord.
intents.members = True # enables members intents on the bot.
bot = commands.Bot(command_prefix=prefix, intents=intents)
After all of that here is your fixed code.
#bot.event
async def on_member_join(member):
embed = discord.Embed(title="Welcome to my server!", description=None, color = discord.Color.magenta())
embed.add_field(name="To get started:", value="•Invite some friends!\n•Check out some of the channels and get engaged with the community!\n•Have fun!", inline=True)
await member.send(embed=embed)
I hope this helped, Have a nice day, Best of luck on your bot.
Here is a Discord Server for Beginners.
🔗 Discord.py For Beginners : https://discord.gg/C8zFM3D2rn

Discord bot won't mute all people

It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?
#bot.command()
async def mute(ctx):
voice_client = ctx.guild.voice_client #
if not voice_client:
return
channel = voice_client.channel
people = channel.members
for person in people:
if person == client.user:
continue
await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
edit Full code:
import discord
from discord.ext import commands
import os
from discord.utils import get
client = discord.Client()
DISCORD_TOKEN = os.getenv("TokenGoesHere")
bot = commands.Bot(command_prefix="$")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#bot.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#bot.command()
async def disconnect(ctx):
channel = ctx.message.author.voice.channel
await channel.disconnect()
#bot.command()
#commands.has_permissions(administrator=True)
async def mute(ctx):
voice_client = ctx.guild.voice_client #get bot's current voice connection in this guild
if not voice_client: #if no connection...
return #probably should add some kind of message for the users to know why nothing is happening here, like ctx.send("I'm not in any voice channel...")
channel = voice_client.channel #get the voice channel of the voice connection
people = channel.members #get the members in the channel
for person in people: #loop over those members
if person == client.user: #if the person being checked is the bot...
continue #skip this iteration, to avoid muting the bot
await person.edit(mute=True, reason="{} told me to mute everyone in this channel".format(ctx.author))
#edit the person to be muted, with a reason that shows in the audit log who invoked this command. Line is awaited because it involves sending commands ("mute this user") to the server then waiting for a response.
#bot.command()
#commands.has_permissions(administrator=True)
async def unmute(ctx):
voice_client = ctx.guild.voice_client
if not voice_client:
return
channel = voice_client.channel
people = channel.members
for person in people:
if person == client.user:
continue
await person.edit(mute=False, reason="{} told me to mute everyone in this channel".format(ctx.author))
bot.run("TokenGoesHere")
Hope this helps, Bot sometimes mutes only itself or only itself and other user, but specifically that one user...
It only mutes me and itself when it shouldn't, bot has highest role and also has permission in joined voice channel. any ideas?
You have to define the intents to use some events, functions like on_message, guild.members, channel.members etc.
# Here is your imports
import discord
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='', intents=intents)
# Rest of your code
Also, you have to activate intents from Discord Developer Portal.
Go to your bot application.
Go to Bot -> Privileged Gateway Intents.
Activate Presence Intent and Server Members Intent.

Discord bot deleting all messages when trigger word is found

Hey I'm making a discord bot that detects trigger words from a list and then deletes that message and whenever it is detecting a message it will keep deleting every message afterwards, i can't figure out why its doing this.
import discord
with open("triggerWords.txt") as file:
triggers = [trigger.strip().lower() for trigger in file.readlines()]
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if any(trigger in message.content for trigger in triggers):
await message.delete(limit=1)
client.run('input_token')
message.delete doesn’t take the limit kwarg, simply do:
await message.delete()
Also I’m pretty sure this shouldn’t even work, you don’t have any intents enabled and on_message requires intents.messages

Discord bot number of guilds in presence

is possible to make Python code that will display the number of servers the bot is a member of in presence status?
For example "watching: 1234 servers"
If someone invites the bot on the next server it will change.
Create an Activity with type watching, and use change_presence to assign it to your bot:
from discord import Activity, ActivityType
from discord.ext import commands
bot = commands.Bot("!")
#bot.event
async def on_ready():
await bot.wait_until_ready()
await bot.change_presence(activity=Activity(name=f"{len(bot.guilds)} servers",
type=ActivityType.watching))
bot.run('TOKEN')

Categories

Resources