i am making an embed and trying to use the argument user has to pass in, the number has to be in range(1,13) 1 - 12, can't check if it is range, it's like return only False
#client.command()
async def video(ctx, number):
if number == range(1,13):
await ctx.send('ready')
else:
await ctx.send(f'There are only {len(videos)} video(s) on the youtube channel')
Don't worry about videos variable, it just has to check if passed in number in the range.
Than you
The number argument is a string you can define at as an int as shown below.
Also you should compare using in becasue you can't equate a number to a range.
Here is the code fixed.
#client.command()
async def video(ctx, number: int):
if number in range(1,13):
await ctx.send('ready')
else:
await ctx.send(f'There are only {len(videos)} video(s) on the youtube channel')
Related
I am trying to add a status in my discord bot, which shows the total number of members, the bot is watching like all bots show for exmaple.
If my bot is added in 2 servers, 1st one has 20 members, second one has 30 members.
= 50 Members Total, How Can I show this???? Can anybody help??
Proof That The Code Is 100% Working
def find_members():
l = str(client.guilds)
l = l.split(" ")
f = 0
for i in range(len(l)):
if "member_count=" in l[i]:
s = l[i]
d = ""
for i in s:
if i.isnumeric():
d += i
else:
pass
f += int(d)
else:
pass
return f
Your function can be simplified significantly. Guild has a member_count property which is the number of members in the guild. You do have to have the members intent enabled for that though. But using that, getting the member count is trivial.
There's also no need to make the list of guilds a list and then make it a list again - just iterate over it directly. With Python, we can iterate over the items in a list doing for thing in things so we don't need to use range here either; there's no real need to know the index of the guild in the list.
def find_members(client: discord.Client) -> int:
total_member_count = 0
for guild in client.guilds:
total_member_count += guild.member_count
return total_member_count
You can now call the find_members function and pass in the client (or remove it and rely on the client already defined in the file).
I've found a solution which you can use to get the total count of members a bot is watching
def find_members():
l = str(client.guilds)
l = l.split(" ")
f = 0
for i in range(len(l)):
if "member_count=" in l[i]:
s = l[i]
d = ""
for i in s:
if i.isnumeric():
d += i
else:
pass
f += int(d)
else:
pass
return f
For Proof Checkout The Image URL In The Question!
so there is a easy method by using bot.users function, the only thing you need is to enable server member intent on the discord bot dashboard and then just add this piece of code,
len(bot.users)
#bot.command()
async def strike(ctx, member : discord.Member):
count = 0
if (member.id == <id here>) and (count < 3):
count = count + 1
i am pretty new to python in general and have only used it to create a discord bot. When the strike command is run, it sets count = to 0 and therefore the count variable cannot go higher than 1. If I move the variable assignment outside of the command I get an error saying that the variable was referenced before it was assigned.
How can I change my code to add 1 to the variable every time the command is run without reassigning the variable every time the command is run?
I removed a lot of the code so the question is less cluttered, I believe this is the only issue preventing this command from working.
You need to make your variable as a global variable.
bot.count = 0
#bot.command()
async def strike(ctx, member : discord.Member):
if (member.id == <id here>) and (bot.count < 3):
bot.count += 1
You can also do:
count = 0
#bot.command()
async def strike(ctx, member : discord.Member):
global count
if (member.id == <id here>) and (count < 3):
count += 1
I don't know what are you trying to do here but I'm only fixing your code
I want to make my bot respond in a single message, but because of the for loop it replies 10 times in 10 different messages. My code is:
#bot.command(name='translate', help='- Translates text to a random language!')
async def translating(ctx, thing):
translator = Translator()
for i in range(0, 10):
translation = translator.translate(thing, dest=str(random.choice(list_language_unicodes)))
await ctx.reply(translation.text)
The code takes a string and translates it to 10 random different languages if somebody was interested. My problem is only with the bot NOT replying in a SINGLE message. I'm a beginner so i probably won't understand much but still.
Solution: I concatenated the strings and reset at the end of the loop because the translations kept stacking:
for i in range(0, 10):
translation = translator.translate(thing, dest=str(random.choice(list_language_unicodes)))
finalny_tekst = finalny_tekst + translation.text + "\n"
await ctx.reply(finalny_tekst)
finalny_tekst = ""
Thanks for assistance!
I'm trying to make a simple bot that would only react to messages with attachments. Then after a certain time, it would make a link to the message and send it on a moderation channel if it got 2 or more reactions.
#client.event
async def on_message(message):
if message.channel.id == 828579458167996420:
if message.attachments or "http" in message.content:
msgID = message.id
await message.add_reaction("<:uut:828580756384120912>")
await asyncio.sleep(200)
x = int
if message.reactions.count(x) >= 3:
link = 'https://discord.com/channels/11223345678900/828579458167996420/' + str(msgID)
channel = client.get_channel(892065611876823100)
x = x - 1
await channel.send("this post " + str(link) + "is liked " + str(x) + "times." )
the bot reacts to the messages i want, but it doesn't post anything in the moderation channel
I'm a beginner, sry for the messy codes :\
If you are not getting any kinds of errors, the issue lays right here, the if statement never gets triggered:
x = int
if message.reactions.count(x) >= 3:
message.reactions will return a list, your statement would count how many times x is in that list (which is always zero).
What you wanna do instead is just to get the total length of that list, like this:
if len(message.reactions) >= 3:
Also other small improvement tip: You can get the URL of a message with message.jump_url
EDIT:
Sorry, what you actually need to do to get the reaction count is to either search the emoji you want to count, or the shortcut would be to just get the first reaction in that list, since your bot should be the first to react to that message anyways:
#this will return a list of reactions that match the emoji you want
emoji = [x for x in message.reactions if str(x.emoji) == '😂'] #replace the emoji with the one you want
print(emoji[0].count) #counts the first (and only) emoji in the new list
"Shortcut" version, just gets the first reaction count:
print(message.reactions[0].count)
Trying to get bot to send a message when the person sends a message in range
async def on_message(message):
if 0 < Message < 100 in message.content:
await bot.message.send(message.channel, "you are in Bronze 1")
Ok first of all. Please don't code on mobile. Especially not python. That will mess things up way too much.
Second, please define your variables properly. Message is not a defined variable. It will return an error.
So, as you said, you wanted two ways to do this. The number should be the message itself, or within the message.
For the first example, all you have to do is cast the message.content to int. Then you can check if it's in the range.
if 0 < int(message.content) < 100:
...
For the second example, you will have to do something similar, however, you should split the entire message.content string and convert the number ones into integers. I'm assuming that the number will not be within a word and it will be by itself.
for word in message.content.split():
if word.isnumeric():
num = int(word)
if 0 < num < 100:
... # return and send message to avoid spamming