how i can count members in python i need full code - python

i want to count members in discord but how
using async
await message.channel.send(f"""# of Members: {id.member_count}""")
i try
#client.event
async def on_message(message):
#id = client.get_guild(ID)
if message.content.find("!hello") != -1:
await message.channel.send("Hi")
elif message.content == "!users":
await message.channel.send(f"""# of Members: {id.member_count}""")
i know this is copy code my code is
#bot.command()
async def countmember(ctx):
ctx.guild.members
len(ctx.guild.members)
await ctx.send(f""" of member: {id.member_count}""")

To get the amount of members from a guild can be retrieved with member_count, with this you also need to properly define guild or just simply using ctx.guild.member_count
This is an example in your command, I would also recommend to use a command instead of an on_message event to use as a command, there's just loss of benefits using that way.
#bot.command()
async def countmember(ctx):
guild = ctx.guild
await ctx.send(f"Member count: {guild.member_count}")
Even if you still wanted to get the guild count with an on_message, it can be done this way,
#bot.event
async def on_message(message):
guild = message.guild
if message.content.find("!hello") != -1:
await message.channel.send("Hi")
elif message.content == "!users":
await message.channel.send(f"""# of Members: {guild.member_count}""")
You have also used bot and client you should only be using one but i'd assume your code works

Related

on_member_join not triggering

I'm using discord.py to write a bot. I want it to add a role named 'unverified' to all members upon joining. I don't get any error but the role just doesn't get added for some reason
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(member.guild.roles, name="unverified")
await member.add_roles(role_unverified)
from discord.utils import get
#client.event
async def on_member_join(member):
role = get(member.guild.roles, name="unverified")
await member.add_roles(role)
this should work i think
aren't you retrieving the role from the member you're trying to assign it to?
Woulnd't it be something like
#client.event
async def on_member_join(member):
role_unverified = discord.utils.get(guild.roles, name="unverified")
await member.add_roles(role_unverified)

how to check if message is in a specific server (discord.py)

command I tried:
#bot.event
async def on_message(message):
if message.channel.server.id == '783763061907527':
if message.content.startswith('!test'):
await message.channel.send('this is a test')
why it isn't working?
server was renamed to guild in discord.py 1.0+. Also IDs are integers, not strings.
#bot.event
async def on_message(message):
if message.guild.id == 783763061907527:
if message.content.startswith('!test'):
await message.channel.send('this is a test')
Guild, Channel, Message or User's id is an integer. And startswith is a function and using like in below.
#bot.event
async def on_message(message):
if message.guild.id == 783763061907527:
if message.content.startswith('!test'):
await message.channel.send('this is a test')

discord.py - check multiple guilds id

Okay so I'm trying to make a command that is only available for specific guilds.
Here's The code.
If I add multiple guild IDs then every guild can use this command.
async def is_guild(ctx):
return ctx.guild.id == someguildidhere, someguildidhere
#client.command()
#commands.check(is_guild)
async def checkispremium(ctx):
await ctx.send("Guild owns lifetime premium.")
#checkispremium.error
async def checkispremium(ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("Guild doesn't owns premium ")
However when I add only one guild ID then everything works fine. Only the specific guild can use the command and others will get error.
async def is_guild(ctx):
return ctx.guild.id == someguildidhere
#client.command()
#commands.check(is_guild)
async def checkispremium(ctx):
await ctx.send("Guild owns lifetime premium.")
#checkispremium.error
async def checkispremium(ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send("Guild doesn't owns premium")
Anyone know how can I have multiple guild IDs, I tried looking at discordpy-rewrite docs, but looks like nothing is there.
Use
async def is_guild(ctx):
return ctx.guild.id in [someguildidhere, someguildidhere, ...]

Discord.py - Bot not sending messages & no error messages

so I'm working on making my own bot for my server and after a while I finally found a string for autorole & role assignment that worked.
I then kept on adding another string for the bot simply replying "Hello". As soon as I add that the role commands won't work anymore. Once I take it out it works again.
On the other hand I have a 8ball and a dice roll command that works with and without the Hello Command
I have no idea what is the problem...
#client.event
async def on_member_join(member):
channel = discord.utils.get(member.guild.channels, name='entrance')
await channel.send(f'Welcome {member.mention} to Dreamy Castle! \n Please make sure to read the rules!')
role = discord.utils.get(member.guild.roles, name="Peasants")
await member.add_roles(role)
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
#client.event #this is the hello command
async def on_message(message):
message.content.lower()
if message.content.startswith('Hello Conny'):
await message.channel.send('Hello!')
Use if and elif not 2 different functions for same event.
Also you might need commands.Bot for a fully functional commanded bot.
#client.event
async def on_message(message):
if message.content.startswith('+acceptrules'):
member = message.author
role1 = discord.utils.get(member.guild.roles, name='The People')
await member.add_roles(role1)
elif message.content.lower().startswith("hello conny"):
await message.channel.send("Hello!")
await client.process_commands(message)

How do i use GuildChannel.Delete, with discord.py?

so, im coding my first discord bot, and i want it to do something similar to the bot Xenon where it deletes all the channels it can, but i have no idea how to do so. this is what i have so far
#client.event
async def on_message(message):
if message.content == "delete channels":
await GuildChannel.Delete
You need to get the Guild channel from the message object
ie:
channel = message.channel # The channel the message was sent
await channel.delete()
so with your code:
#client.event
async def on_message(message):
if message.content == "delete channels":
channel = message.channel
await channel.delete()

Categories

Resources