Discord bot number of guilds in presence - python

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

Related

Why is my Discord bot refusing to respond to commands now when it responded before?

My bot suddenly stopped responding to commands. I believe it's on Discord's end as any time I attempt to invite the bot, permissions only say "This will allow the developer to: Create commands in your server." I have a second bot that I haven't touched since the week it was made and it is having no problems. Am I doing something wrong in the developer portal? It's a super basic bot that sends text at the moment.
import discord
import os
from keep_alive import keep_alive
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
message.content
if "p.h" in message.content:
await message.channel.send('Hello!')
keep_alive()
client.run(os.getenv('TOKEN'))
Go discord developer portal > select your app > navigate to "bot" in sidebar
Enable message content intent, to allow the bot read message sent from the user

Your message was not delivered. Discord bot

I'm making a discord bot. when I send commands on the server from my account with the owner role, then everything is OK, it is executed.
photo_1
If I switch to another account that has the everyone role, then the bot stupidly does not react.
photo_2
here is a piece of code:
import discord
from discord.ext import commands
from dislash import InteractionClient, Option, OptionType
bot = commands.Bot(command_prefix='!')
inter_client = InteractionClient(bot)
#bot.event
async def on_ready():
print('Bot is launch!')
#inter_client.slash_command(description="Says Hello")
async def hello(ctx):
await ctx.respond(content='asfd', ephemeral=True)
bot.run(token)

Discord Py Need to give my bot role silently in background

I still have not figured out how to add/remove roles on the bot itself, without executing commands in the chat. It has to be silently in the background. Can someone please help me.
Current code below that does work but not the way I want it to. I do not want the bot to type a command to give itself a role, I need it to automatically give it silently without any chat messages being executed. My ideal idea is to execute the addRole function in the on_ready event somehow, without sending message to get a role.
import aiohttp
from datetime import datetime
from dotenv import load_dotenv
from discord.ext import commands
import discord
import os
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
bot = commands.Bot(command_prefix="$")
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.wait_until_ready()
bot.session = aiohttp.ClientSession()
await bot.get_channel(402353715718277809).send("$addRole")
#bot.command()
async def addRole(ctx):
role = discord.utils.get(ctx.guild.roles, name="market_green")
member = ctx.guild.get_member(bot.user.id)
await member.add_roles(role)
bot.run(DISCORD_TOKEN)
The guild object in discord.py has a .me attribute (docs) which represents yourself in that guild.
So on start you can get that guild based on it's id (in the on_ready event) and assign yourself a role with add_roles, example code(with guildid the id of the guild you want the role in):
guild = bot.get_guild(guildid)
role = discord.utils.get(guild.roles, name="market_green")
await guild.me.add_roles(role)

Discord bot not wroking

I am currently learning how to make a discord bot using python. But I am stuck at the beginning. My bot is not responding.
It is NOT showing an error. Also in the discord server the bot is showing online.
But when I run guild.member_count it shows the correct number of members. But when I try to get the information of the members by guild.members, it just shows my bot in the list.
Moreover If I try to send message by await member.create_dm() in on_member_join(), it don't send any message.
Also I gave the bot administrator permission to see if its some problem with permissions but still the same.
Below is my code :
import discord
TOKEN = <MyToken> # I have replaced this with my actual token in the actual code
client = discord.Client()
#client.event
async def on_ready():
guild = discord.utils.get(client.guilds, name=GUILD)
print(
f"{client.user} is connected to Discord!\n"
f"Connected to {guild.name} (id: {guild.id}, members-count: {guild.member_count})"
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
#client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hello {member.name}, Welcome to the test discord server!"
)
print(f"Welcomed {member.name}.\n")
client.run(TOKEN)
it just shows my bot in the list.
See this page on Gateway Intents
it don't send any message
Try just using
await member.send(...)

Why won't Discord bot recognize commands?

I'm making a bot for a pokemon server, and I'm trying to make a command that will give the 'Gym Leader' role to another user. I try using the command, and using the test command, but there is no response in the server nor the shell.
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.utils import get
bot = commands.Bot(command_prefix='b!', case_insensitive=True)
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event #works
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
channel = client.get_channel(697500755766018098)
#client.event #works
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, welcome to Pokémon Beast Ball!\n\nThis server utilizes Pokecord and Mewbot.\n\nSay \'pkhelp\' in the server to learn about Pokecord commands.\nSay \';help\' in the server to learn about Mewbot commands.'
)
#bot.command() #doesn't work
async def test(ctx):
print("test recieved")
await ctx.send(ctx)
#bot.command(pass_context=True) #this is the command that really needs help
async def newleader(ctx: discord.User=None):
print("command recieved")
if not user:
await ctx.send("Invalid")
print("1")
else:
role = discord.utils.get(ctx.guild.roles, name="Gym Leader")
role2 = discord.utils.get(ctx.guild.roles, name="Purple UniTurtle Man")
if role in ctx.author.roles or role2 in ctx.author.roles:
print("2")
await ctx.send(f'A new Gym Leader has been appointed')
await user.add_roles(role)
await bot.remove_roles(ctx.author, role)
else:
print("3")
await ctx.send("You do not have permission to use this command")
client.run(TOKEN)
You are mixing bot and client and your client = discord.Client() is stepping on your bot = commands.Bot(...) statement. Since you want to do commands and events you only use the commands.Bot(...) statement.
Remove the client = discord.Client() statement and change your #client.event decorators to #bot.event.
Also if you want to reference the command context in your test command update it with the ctx parameter async def test(ctx):.
That will get you started using your commands and entering b1test will now work.
Please note that the case_insensitive=True on the commands declaration refers to the command name and not the prefix.
Did you check :
bot connection → make a "on_connect" event (not exactly the same as "on_ready") to see if your bot successfully connect to your server (before receiving data from discord). If not, try to add your bot again to your server and check if all tokens are goods.
bot permissions (if your bot have the right to write in channel, read messages from channels, manage roles) → if your bot cannot read messages, he can't read commands !
role priority (you can't manage roles highers thant yours) → go to "server settings" > "roles" > put your bot role above the 'Gym Leader' role (or at the top of the list if you don't care).
The problem isn't actually what the selected answer suggests. There is probably no reason to use both commands.Bot and discord.Client but using both won't lead to that issue.
The issue is because you are only running the client, not the bot. You need to also run the bot instance if you want it to function.
If you are not trying to do something specific, then just using either bot or client will suffice anyway, so that part of the selected answer was helpful in avoiding the issue at least.

Categories

Resources