my discord bot needs a giveaway command. it is correctly sending the giveaway message, but for some reason, it isn't capable to determine the winner. this is the command:
#commands.command()
#commands.has_permissions(administrator=True)
async def giveaway(self, ctx, time, winners: int, prize):
channel = ctx.channel
embed = nextcord.Embed(title="Giveaway!",description=f"{prize}",color=nextcord.Color.green())
embed.add_field(name="Giveaway de:", value=ctx.author.mention)
embed.set_footer(text=f"Fini dans {time}!")
msg = await ctx.channel.send(embed=embed)
await msg.add_reaction("🎉")
d = time[-1]
num = int(time[:-1])
if d == "s":
await asyncio.sleep(num)
elif d == "m":
await asyncio.sleep(num * 60)
elif d == "h":
await asyncio.sleep(num * 60 * 60)
elif d == "d":
await asyncio.sleep(num * 60 * 60 * 24)
winners_number = winners[:-1]
new_msg = await channel.fetch_message(msg.id)
users_mention = []
for i in range(winners_number):
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.bot.user))
winner = random.choice(users)
users_mention.append(winner.mention)
users.remove(winner)
await ctx.send(f"Congratulations {users_mention} you won the giveaway !")
error:
Related
def convert(time):
time_dict = {"s": "seconds", "m": "minutes", "h": "hours", "d": "days"} # pulling your idea, but replacing the seconds with the full name of the acronym.
val = time[:-1] # everything before the last character
unit = time[-1] # the last character
return f"{val} {time_dict[unit]}"
convert("5d") # 5 days
convert("10s") # 10 seconds
#client.command()
#commands.has_permissions(manage_messages=True)
async def gcreate(ctx, time: str, *, prize: str):
time = convert(time)
embed = discord.Embed(title=prize,
description=f"Hosted by - {ctx.author.mention} React with :tada: to enter! Time Remaining: **{time}**",
colour = discord.Colour.purple())
msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
await msg.add_reaction("🎉")
await asyncio.sleep(3)
await asyncio.sleep(int(time))
new_msg = await ctx.channel.fetch_message(msg.id)
user_list = [user for user in await new_msg.reactions[0].users().flatten() if
user != client.user]
if len(user_list) == 0:
await ctx.send("No one reacted.")
else:
winner = random.choice(user_list)
await ctx.send(f"{winner.mention} You have won the {prize}!")
I get no errors, it just won't send the winner message.
I am doing a giveaway command call =gstart <time> <prize and the giveaway will be held in the channel I use this command and After sending the embed giveaway message it will delete the gstart command. Here is the code:
#bot.command()
async def gstart(ctx,time,*,prize):
time = convert(time)
if time == -1:
await ctx.send("Error, can't identify time unit")
if time == -2:
await ctx.send("Error, can't identify time value")
embed = discord.Embed(title=prize, description = f"React with :tada: to enter\nHosted by: {ctx.author.mention}", color=discord.Color.red())
embed.set_footer(text = f"Ends at {time} from now!")
#delete-command
await ctx.send(":tada: **GIVEAWAY** :tada:")
my_msg = await ctx.send(embed = embed)
await my_msg.add_reaction("🎉")
await asyncio.sleep(time)
new_msg = await ctx.fetch_message(my_msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(bot.user))
winner = random.choice(users)
await ctx.send(f"Congratulations {winner.mention}! You won the {prize}!")
msg_link = my_msg.jump_url
winner_embed = discord.Embed(title=f"Hey {winner}",description=f"You just won a [giveaway]({msg_link}) in {ctx.guild}",color=discord.Color.red())
winner_embed.add_field(name=prize,value=f"Hosted by: {ctx.author.mention}",inline=True)
await winner.send(embed=winner_embed)
By the way the convert() function looks like this:
def convert(time):
pos = ["s","m","h","d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
Reply you with the discord.Context object and delete the message user sent.
Example:
import asyncio
#bot.command()
async def deleteCommandAfterSent(ctx):
await ctx.send("blablabla ") #REPLY MESSAGE
await asyncio.sleep(1)
await ctx.message.delete()
Here is the code I have, the time is not being converted, and I don't know what to do anymore. If you know what to do, let me know how to do this
Here is what I got so far:
def convert(time):
pos = ["s","m","h","d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d": 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
#---------------------------------------------------------------------------
#client.command()
#commands.has_permissions(manage_messages = True)
async def giveaway(ctx, time : str, *, prize: str):
embed = discord.Embed(title=prize,
description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{time}** seconds",
color=ctx.guild.me.top_role.color, )
msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
await msg.add_reaction("🎉")
await asyncio.sleep(3)
new_msg = await ctx.channel.fetch_message(msg.id)
user_list = [u for u in await new_msg.reactions[0].users().flatten() if u != client.user] # Check the reactions/don't count the bot reaction
if len(user_list) == 0:
await ctx.send("No one reacted.")
else:
winner = random.choice(user_list)
await ctx.send(f"{winner.mention} You have won the {prize}!")
When I type in 2m meaning 2 minutes, it shows 2m seconds remaining, now I know why it says seconds because I have not updated the response yet, but the time is only 2 seconds plus the 3 seconds delay time. Basically a total of around 6 seconds.
I did just throw 2 commands from stack overflow together, and it is like putting in a Lamborghini head gasket and a dodge engine block, I know it should not work, even with the little bit of modifications, I kinda do see whats wrong now, but I don't know how to fix it
So I revised your code and changed the giveaway command a little bit. After a few modifications, the command worked as it should for me. Here is how I re-defined it:
def convert(time):
pos = ["s", "m", "h", "d"]
time_dict = {"s": 1, "m": 60, "h": 3600, "d": 3600 * 24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
# ---------------------------------------------------------------------------
#client.command()
#commands.has_permissions(manage_messages=True)
async def giveaway(ctx, time: str, *, prize: str):
time = convert(time)
embed = discord.Embed(title=prize,
description=f"Hosted by - {ctx.author.mention}\nReact with :tada: to enter!\nTime Remaining: **{time}** seconds",
color=ctx.guild.me.top_role.color)
msg = await ctx.channel.send(content=":tada: **GIVEAWAY** :tada:", embed=embed)
await msg.add_reaction("🎉")
await asyncio.sleep(3)
await asyncio.sleep(int(time))
new_msg = await ctx.channel.fetch_message(msg.id)
user_list = [user for user in await new_msg.reactions[0].users().flatten() if
user != client.user] # Check the reactions/don't count the bot reaction
if len(user_list) == 0:
await ctx.send("No one reacted.")
else:
winner = random.choice(user_list)
await ctx.send(f"{winner.mention} You have won the {prize}!")
You can use this for converting time.
import re
from discord.ext.commands import BadArgument
time_regex = re.compile(r"(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}
def convert(argument):
args = argument.lower()
matches = re.findall(time_regex, args)
time = 0
for key, value in matches:
try:
time += time_dict[value] * float(key)
except:
raise BadArgument
return round(time)
I was trying to create a discord bot with giveaway command which updates time left every 1-2 mins in giveaway embed . I have been trying it since 3 days but wasn't able to find a solution. I managed to make it update seconds, but if I specify time more than 1m i.e. 60 seconds it automatically converts it to seconds and starts giveaway with time left in just seconds . I want it to keep time in given unit and update time in --days, --hours, --minutes, --seconds left.
Here are a few images what i mean:
What it currently do:
What i want it to do:
Its just Ends In Or Time remaining what i want to be changed!
My current code:
#commands.command()
#commands.guild_only()
async def gstart(self, ctx, duration, *, prize):
time = self.convert(duration)
if time == -1:
await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
return
elif time == -2:
await ctx.send(f'Time Must Be A Integer!')
return
giveawayembed = discord.Embed(
title="🎉 New Giveaway! 🎉",
description=f"**Prize:** {prize}\n"
f"**Hosted By:** {ctx.author.mention}\n"
f"**Ends In:** {time} Seconds",
colour=discord.Color.green()
)
msg = await ctx.send(embed=giveawayembed)
reactions = await msg.add_reaction("🎉")
while time:
await sleep(10)
time -= 10
giveawayembed.description= f"**Prize:** {prize}\n**Hosted By:** {ctx.author.mention}\n**Ends In:** {time} Seconds"
await msg.edit(embed=giveawayembed)
new_msg = await ctx.fetch_message(msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.client.user))
winner = random.choice(users)
endembed = discord.Embed(
title="Giveaway ended!",
description=f"Prize: {prize}\nWinner: {winner.mention}")
await msg.edit(embed=endembed)
await ctx.send(f"🎉 Giveaway Winner: {winner.mention} | Prize: {prize}")
For Converting Time I Have:
class Giveaway(commands.Cog):
def __init__(self, client):
self.client = client
def convert(self, time):
pos = ["s", "m", "h", "d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except:
return -2
return val * time_dict[unit]
Any help is highly appreciated! 😁
And I m sorry if I wasn't able to make you understand my problem. 😅
I understand your problem as it is actually a very simple fix, you can either import time, or use await asyncio.sleep(time)
Before using the code I am providing, make sure to import asyncio in your imports.
Your code:
#commands.command()
#commands.guild_only()
async def gstart(self, ctx, duration, *, prize):
time = self.convert(duration)
if time == -1:
await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
return
elif time == -2:
await ctx.send(f'Time Must Be A Integer!')
return
giveawayembed = discord.Embed(
title="🎉 New Giveaway! 🎉",
description=f"**Prize:** {prize}\n"
f"**Hosted By:** {ctx.author.mention}\n"
f"**Ends In:** {time} Seconds",
colour=discord.Color.green()
)
msg = await ctx.send(embed=giveawayembed)
reactions = await msg.add_reaction("🎉")
while time:
await sleep(10)
time -= 10
giveawayembed.description= f"**Prize:** {prize}\n**Hosted By:** {ctx.author.mention}\n**Ends In:** {time} Seconds"
await msg.edit(embed=giveawayembed)
new_msg = await ctx.fetch_message(msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.client.user))
winner = random.choice(users)
endembed = discord.Embed(
title="Giveaway ended!",
description=f"Prize: {prize}\nWinner: {winner.mention}")
await msg.edit(embed=endembed)
await ctx.send(f"🎉 Giveaway Winner: {winner.mention} | Prize: {prize}")
In both fixes I will fix a slight problem that could easily be fixed, you have it so while time, it will go down by 10, I recommend doing it so it says while time > 0: so that once it hits 0 it wont keep counting down.
Easy fix using await asyncio.sleep(time):
#commands.command()
#commands.guild_only()
async def gstart(self, ctx, duration, *, prize):
time = self.convert(duration)
if time == -1:
await ctx.send(f'Answer Time With A Proper Unit (s, m, h, d)')
return
elif time == -2:
await ctx.send(f'Time Must Be A Integer!')
return
giveawayembed = discord.Embed(
title="🎉 New Giveaway! 🎉",
description=f"**Prize:** {prize}\n"
f"**Hosted By:** {ctx.author.mention}\n"
f"**Ends In:** {time} Seconds",
colour=discord.Color.green()
)
msg = await ctx.send(embed=giveawayembed)
reactions = await msg.add_reaction("🎉")
while time >= 0:
if time <= 60:
giveaway.remove_field(index=1)
giveaway.insert_field_at(index=1, name='Ends:', value=f'{time} second(s) from now')
await my_msg.edit(embed=giveaway)
time -= 10
await asyncio.sleep(10)
elif 60 <= time < 3600:
giveaway.remove_field(index=1)
giveaway.insert_field_at(index=1, name='Ends:', value=f'{time/60} minute(s) from now')
await my_msg.edit(embed=giveaway)
time -= 6
await asyncio.sleep(6)
elif 3600 <= time < 86400:
giveaway.remove_field(index=1)
giveaway.insert_field_at(index=1, name='Ends:', value=f'{time/3600} hour(s) from now')
await my_msg.edit(embed=giveaway)
time -= 360
await asyncio.sleep(360)
elif time >= 86400:
giveaway.remove_field(index=1)
giveaway.insert_field_at(index=1, name='Ends:', value=f'{time/86400} day(s) from now')
await my_msg.edit(embed=giveaway)
time -= 8640
await asyncio.sleep(8640)
if time <= 0:
giveaway.remove_field(index=1)
giveaway.insert_field_at(index=1, name='Ends:', value=f'Ended at {datetime.datetime.now().strftime("%B %d, %I:%M %p")}') # noqa
await my_msg.edit(embed=giveaway)
await asyncio.sleep(time)
new_msg = await ctx.fetch_message(msg.id)
users = await new_msg.reactions[0].users().flatten()
users.pop(users.index(self.client.user))
winner = random.choice(users)
endembed = discord.Embed(
title="Giveaway ended!",
description=f"Prize: {prize}\nWinner: {winner.mention}")
await msg.edit(embed=endembed)
await ctx.send(f"🎉 Giveaway Winner: {winner.mention} | Prize: {prize}")
If you are still confused or have any more questions you can contact me on discord at killrebeest#7187 or comment on this answer. Have a good day!
i want to make something like time convert in my discord bot. Now, to use tempmute command, i need to set time only in seconds, and i want to make converting e.g. 1s = 1; 1h = 3600 etc.
Answer, what i found, do not solving my problem.
Here's my code:
# tempmute
#client.command()
#commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
if not member or time == 0 or time == str:
return
elif reason == None:
reason = 'no reason provided'
tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
tempmuteembed.set_footer(text=f"{ctx.guild.name} • {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
tempmuteembed.add_field(name='Reason:', value=f"{reason}")
tempmuteembed.add_field(name='Duration:', value=f"{time}")
tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)
await ctx.channel.purge(limit=1)
guild = ctx.guild
for role in guild.roles:
if role.name == 'Muted':
await member.add_roles(role)
await ctx.send(embed=tempmuteembed)
await asyncio.sleep(time)
await member.remove_roles(role)
return
If your input may be 15s, 20min, 1h, 2d, your possible solution is:
import re
def tempmute(time=0):
time_list = re.split('(\d+)',time)
if time_list[2] == "s":
time_in_s = int(time_list[1])
if time_list[2] == "min":
time_in_s = int(time_list[1]) * 60
if time_list[2] == "h":
time_in_s = int(time_list[1]) * 60 * 60
if time_list[2] == "d":
time_in_s = int(time_list[1]) * 60 * 60 * 60
return time_in_s
print(tempmute("15h"))
>>> 54000
Update: so the complete code should look something like this. Your Input will be a string, dont return None if its not a string! You input will be in the form of like 15min, 3s or 5h, otherwise the timeout will be 0 seconds.
# tempmute
#client.command()
#commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
if not member or time == 0:
return
elif reason == None:
reason = 'no reason provided'
try:
if time_list[2] == "s":
time_in_s = int(time_list[1])
if time_list[2] == "min":
time_in_s = int(time_list[1]) * 60
if time_list[2] == "h":
time_in_s = int(time_list[1]) * 60 * 60
if time_list[2] == "d":
time_in_s = int(time_list[1]) * 60 * 60 * 60
except:
time_in_s = 0
tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
tempmuteembed.set_footer(text=f"{ctx.guild.name} • {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
tempmuteembed.add_field(name='Reason:', value=f"{reason}")
tempmuteembed.add_field(name='Duration:', value=f"{time}")
tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)
await ctx.channel.purge(limit=1)
guild = ctx.guild
for role in guild.roles:
if role.name == 'Muted':
await member.add_roles(role)
await ctx.send(embed=tempmuteembed)
await asyncio.sleep(time_in_s)
await member.remove_roles(role)
return