discord.py - check multiple guilds id - python

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, ...]

Related

Not able to ban members out of the server .py cog

#commands.command()
#commands.guild_only()
#commands.has_guild_permissions(ban_members=True)
async def ban(self, ctx, member: discord.Member, *, reason=None):
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
channel = self.bot.get_channel(864083000211406849)
embed = discord.Embed(title=f"{ctx.author.name} banned: {member.name}", description=reason)
await channel.send(embed=embed)
await ctx.send(embed=embed)
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
await ctx.guild.ban(user=member, reason=reason)
#ban.error
async def ban_error(self,ctx, error):
if isinstance(error, commands.CheckFailure):
title=f"{ctx.author.mention} you do not have the required permission(s)/role(s) to run this command!"
await ctx.send(title)
So I want my code to basically kick a member that has left my server using their ID. Ive looked in to previous questions but it seems to be no help for me.
You are converting the member input to a discord.Member, which needs to be in the server. Instead convert it to a discord.User, who does not need to be in your server.
async def ban(self, ctx, member: discord.User, *, reason=None):
Please keep in mind that your top role lookup and comparison will not work when you use the discord.User, you will have to get the actual member beforehand, like this:
server_member = ctx.guild.get_member(member.id)
if server_member != None:
if server_member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
Also you will get an error trying to message someone who does not share a server with your bot, or has blocked your bot etc., I would just use a try/except block for this:
try:
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
except discord.errors.Forbidden:
pass

how i can count members in python i need full code

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

How can I kick a user by ID in Discord.py

I was wondering how I could make a command that kicks a specific user every time, without having to mention the user.
I am looking for something like this:
#client.command()
async def kick(ctx):
user = #user id
await user.kick(reason=None)
And not like this:
#client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
Thanks in advance
You can get a member from his/her id by using discord.Guild.get_member.
#client.command()
async def kick(ctx):
user = ctx.guild.get_member(<user id>)
await user.kick(reason=None)
Or you can use discord.utils.get. I'd recommend you to use the first one but this is also a option.
#client.command()
async def kick(ctx):
user = discord.utils.get(ctx.guild.members, id=<user id>)
await user.kick(reason=None)
References
discord.Guild.get_member()
discord.utils.get()
you can use the built in converter for member : discord.Member
like this:
from discord.ext import commands
#client.command()
async def kick(ctx, member: commands.MemberConverter, *, reason):
await ctx.guild.kick(member, reason=reason)
correct me if i'm wrong but it should work
you can watch this video if you're confused: https://www.youtube.com/watch?v=MX29zp9RKlA (this is where i learned all of my discord.py coding stuff)

How do I mention user through discord bots - python

I want to to make some code that will mention the name of the user that sends the message, this is what I have tried:
Im new to making discord bots, and python so any help would be perfect
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {member}")
You can do this with user.name
#client.command()
async def hello(ctx):
await ctx.send(f"hello, {ctx.author.name}")
The above can be called by {prefix}hello, if you want to say "hello, {name}" even when user just send "hello" (without prefix), then use on_message event.
#client.event
async def on_message(message):
if message.author.bot: return
if message.content.lower() == "hello":
await message.channel.send(f"Hello, {message.author.name}")
await client.process_commands(message)
Docs: on_message, user.name
Assuming you want to mention the user who used the command. the ctx argument has a lot of attributes one of them is the author
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {ctx.author.mention}")
If you want the user to say hello to another user, he can mention or just type the name.
#client.command()
async def hello(ctx, *, user: discord.Member = None):
if user:
await ctx.send(f"hello, {user.mention}")
else:
await ctx.send('You have to say who do you want to say hello to')
All of the above providing you are using discord.ext.commands
This should work:
str(ctx.message.author)
Or:
str(ctx.author)

Discord bot can't mention everyone despite having its permission

Here is the sendMessage function:
async def sendMessage(color, title, value, should_delete=True, channel=""):
embed = discord.Embed(color=color)
embed.add_field(name=title, value=value, inline=False)
if channel == "":
msg = await client.send_message(message_obj.channel, embed=embed)
else:
msg = await client.send_message(client.get_channel(channel), embed=embed)
if should_delete:
await delete_msg(msg)
The bot can mention anyone but everyone and here. Despite having mention everyone permission.
sendMessage(OK_COLOR_HASH, "title", "Hello #everyone")
Edit: When I converted the message type to the normal one instead of embed one, it worked.
You can try to sending a mention to everyone through the default_role attribute
#bot.command(pass_context=True)
async def evy(msg):
await bot.say(msg.message.server.default_role)
You can try this block code. roles return a list all guild's roles, but first role always a default guild role (default_role) and that's why you must use slicing function.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.roles[0])
Or you can do something like this.
#bot.command()
async def test(ctx):
await ctx.send(ctx.message.guild.default_role)

Categories

Resources