Discord.py user command counter - discord

I am currently trying to implement a counter to show how many times a user has used a command like "*kiss #randomuser", and then it would show in the embed footer or under title "user has kissed someone {number} of times"
I have tried the solution found here How do I get a counter to work within a discord.py command function?
but nothing I have tried has worked, I apologize but im not every good with this
bot.counter = 0
#bot.command()
##commands.cooldown(1,10,commands.BucketType.user)
async def Random(ctx, member:discord.Member = None):
bot.counter += 1
if member == ctx.author:
em = discord.Embed(title=f'{ctx.author.display_name}, You can not perform this action on yourself ', color = random.randint(0, 16777215) )
await ctx.reply(embed=em)
else:
e = discord.Embed(title=f'{ctx.author.display_name} is doing something random to {member.name}', color = random.randint(0, 16777215))
content = []
link = random.choice(content)
e.set_image(url=f'{link}')
e.set_footer(text=f"This user has used this command {bot.counter}")
await ctx.reply(embed=e)
```

Related

How do I get a list of all members in a discord server, preferably those who aren't bot

I'm attempting checks if a username is in a valid username in the server. The check is done by the following code
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!logan',
intents=intents,
helpCommand=helpCommand)
Users = [# Need help getting users]
async def Fight(ctx, User):
if ctx.author.name != User:
if User in Users:
open_file = open(str(ctx.author.name), "w")
open_file.write(User + "\n300\nTurn")
open_file.close()
open_file = open(User, "w")
open_file.write(str(ctx.author.name) + "\n300\nNot")
open_file.close()
await ctx.author.send("You are now fighting " + User)
#await ctx.User.send("You are now fighting " + ctx.author.name)
else:
await ctx.send("User is not in this server")
else:
await ctx.send("You Cannot Fight Yourself")
I've tried googling, but I haven't found a valid answer
There are other ways to get your guild name, but here's a possible method to get it and get the member list from it.
In member (and user so), there is the ".bot" that is a boolean, 0 if not a bot.
listMembers = []
#bot.command(name="init")
async def FuncInit (ctx) :
global listMembers
guilde = ctx.message.guild
for member in guilde.members :
if not member.bot :
listMembers.append(member.name)

Getting more than 1 post from a sub at a time

reddit = praw.Reddit(client_id='xyz',
client_secret='xyz',
user_agent='xyz',check_for_async=False)
#bot.command(aliases=['memes'])
async def meme(ctx):
memes_submissions = reddit.subreddit('dankmemes').new()
post_to_pick = random.randint(1, 10)
for i in range(0, post_to_pick):
submission = next(x for x in memes_submissions if not x.stickied)
embed = discord.Embed(title= f"Meme-" ,color=0xFF00FF)
embed.set_image(url=submission.url)
'''embed.set_thumbnail(url=ctx.author.avatar_url)'''
embed.set_footer(text='requested by: \n{0}'.format(ctx.author),icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
i wrote this down to extract 1 meme at a time from reddit sub "dankmemes"
i was wondering if theres a way through which i can ask for top 5 memes or get more than just one meme?
any suggestions will be of a lot of help. tysm!
You can get top posts from a subreddit by using the subreddit.top member.
max_top = 5
subreddit = reddit.subreddit('sample_subreddit_with_good_memes')
top = subreddit.top(limit=max_top)
The built-in random library has a function named choices() which gives you a list with multiple randomly selected choices. So you can basically get the top 100 posts and then get 10 random memes from the list.

How to index into the channel.members list and take out the name? Closed

So I'm trying to get the names of all the people in a voice channel, but the bot returns a list like so:
[<Member id=704069756717629472 name='Eedward' discriminator='8402' bot=False nick=None guild=<Guild id=807690875802878003 name='lfg testing' shard_id=None chunked=True member_count=3>>]
Is there any way I can take that "name" part out of the list? Or is there a better way to do something like this? This is my code:
#client.command()
async def lfg(ctx, game, *,extra=None):
if not ctx.message.author.voice:
await ctx.send(f"{ctx.author.name}, you need to connect to a voice channel first to use this command.")
return
else:
channel = ctx.message.author.voice.channel
link = await channel.create_invite(max_age = 300)
members = len(ctx.message.author.voice.channel.members)
members1 = ctx.message.author.voice.channel.members
max_ = channel.user_limit
if max_ == 0:
max_ = 'None'
if max_ == 'None':
p = f'{members}, No Limit'
else:
p = f"{members} out of {max_}"
em = Embed(color=discord.Color.gold())
em.add_field(name='LFG Request', value=f"**{ctx.author.mention} is looking for a group in voice channel `#{channel}`**:\n\n**Game: {game}**\n\n**Extra Info:** {extra}\n\n**Connected Users:**{(members1)} ({p})\n\n**VC Invite: [Click here to join]({link})**")
em.set_footer(text=f'Type !lfg (message) to create this embed | This LFG request was made by {ctx.author.name}')
await ctx.send(embed=em)
Sorry if it's a bit hard to read, thanks in advance! :D (Btw I'm trying to display the names of the users in the vc in the "Connected Users" section of the embed)
Edit: I figured it out, for those who want the code, here it is:
members = len(ctx.message.author.voice.channel.members)
count = 0
members1 = ""
for _ in range(int(members)):
members1 += f"{ctx.message.author.voice.channel.members[count].mention}"
count += 1
try adding .name to the member. This should get the name from the member.

How would i get live updates when a user joins and leaves a discord voice channel

Is there a way too do a callback or a background task to see when a user joins and leaves a voicechannel? Currently when I cmd the bot I only am able to see the users that are currently in the voicechannel only. Sorry if it might not make sense.
import asyncio
import config
client = discord.Client()
#client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
#Voice channel
lobby_voicechannel = client.get_channel(708339994871463986)
#Text channel
txt_channel = client.get_channel(702908501533655203)
team_one = []
team_two = []
member_id = []
lobby_queue = lobby_voicechannel.members
for x in lobby_queue:
#change mention to name or nick for variations
member_id.append(x.mention)
player_num = len(member_id)
joined_user = str(member_id)
#check how many players in total for queue
if player_num == 5:
user_convert = tuple(member_id)
embed = discord.Embed(
title="**{}/10** players joined `{}`".format(player_num, lobby_voicechannel),
description="\n".join(user_convert),
color=0x00f2ff)
await txt_channel.send(delete_after=None, embed=embed)
else:
if player_num == 0:
embed = discord.Embed(
title="**{}/10** players joined `{}`".format(player_num, lobby_voicechannel),
description=f"***```No players in {lobby_voicechannel}```***",
color=0x00f2ff
)
await txt_channel.send(delete_after=None, embed=embed)
client.run(config.Token)```
You can use the on_voice_state_update event. This event gets triggered whenever a member joins/leaves or when a members state changes in a voice channel. For more info see link.
However you still need to check whether or not the member left/joined. This can be done through the before and after VoiceState objects received from the event: on_voice_state_update(member, before, after):.
Example:
#commands.Cog.listener()
async def on_voice_state_update(member, before, after):
if before.channel == None and after.channel != None:
state = 'joined'
else if before.channel != None and after.channel == None:
state = 'left'
else:
state = 'unchanged'
# Continue with some code

Using Discord.py rewrite to delete messages

As the title states, I'm trying to delete messages using my !purge command. I have this down already:
#bot.command()
#commands.has_permissions(manage_messages=True)
async def purge(ctx):
await delete_messages(ctx, member)
await ctx.send("Deleted messages")
It's saying that delete_messages is not defined. Please help me!
So we are in year 2019. I'll help you with your code.
delete_messages is a method of a TextChannel object.
In your line await delete_messages(ctx, member), add ctx.message.channel. just before delete_messages.
Your line would then go like this:
await ctx.message.channel.delete_messages(ctx, member)
Hope that clears things up.
If it did, don't hesitate to 'accept' the answer by clicking the check mark.
This will delete only up to 99 messages (+ the purge command) at a time and the messages must be under 14 days old.
#bot.command(pass_context=True, name='purge', aliases=['purgemessages'], no_pm=True)
async def purge(ctx, number):
number = int(number)
if number > 99 or number < 1:
await ctx.send("I can only delete messages within a range of 1 - 99", delete_after=10)
else:
author = ctx.message.author
authorID = author.id
mgs = []
number = int(number)
channel = ctx.message.channel
async for x in bot.logs_from((channel), limit = int(number+1)):
mgs.append(x)
await delete_messages(mgs)
await ctx.send('Success!', delete_after=4)

Resources