Skip to content

Commit 65e761f

Browse files
committed
Slash commands migration (v0.8)
1 parent acf8859 commit 65e761f

File tree

1 file changed

+78
-92
lines changed

1 file changed

+78
-92
lines changed

cogs/fun.py

+78-92
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import asyncio
2-
31
import nextcord
42
from nextcord.ext import commands
5-
import random
63

4+
import random
5+
import string
76
import requests
87
import datetime
98
from datetime import timezone, timedelta
109
import math
1110
import configparser
1211

12+
from utils.decorators import is_weather_active
13+
1314
config = configparser.ConfigParser()
1415
config.read("config.ini")
1516

@@ -18,10 +19,13 @@ class Fun(commands.Cog):
1819
def __init__(self, client):
1920
self.client = client
2021

21-
@commands.command()
22-
async def dice(self, ctx):
22+
@nextcord.slash_command()
23+
async def dice(self, interaction):
24+
"""
25+
Just a dice.
26+
"""
2327
player_dice, bot_dice = [random.randint(1, 6) for _ in range(2)]
24-
print(player_dice, bot_dice)
28+
2529
if player_dice > bot_dice:
2630
result = "You won!"
2731
color = nextcord.Color.green()
@@ -40,10 +44,13 @@ async def dice(self, ctx):
4044
f"{result}"]),
4145
colour=color)
4246

43-
await ctx.send(embed=embed)
47+
await interaction.send(embed=embed)
4448

45-
@commands.command(aliases=["slots"])
46-
async def roll(self, ctx):
49+
@nextcord.slash_command()
50+
async def roll(self, interaction):
51+
"""
52+
Slots.
53+
"""
4754
emojis = ["🍎", "🍊", "🍐", "🍋", "🍉", "🍇", "🍓", "🍒", "🔔", "💎", ":seven:"]
4855

4956
a = random.choice(emojis)
@@ -64,103 +71,82 @@ async def roll(self, ctx):
6471

6572
slotmachine = nextcord.Embed(title="Slots", description=f"🎰 ({a}|{b}|{c})", colour=color)
6673
slotmachine.add_field(name=name, value=value)
67-
await ctx.send(embed=slotmachine)
68-
69-
@commands.command(aliases=["pass"])
70-
async def password(self, ctx, *, lenght: int = None):
71-
lower = 'abcdefghijklmnopqrstuvwxyz'
72-
upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
73-
digits = '0123456789'
74-
punct = ';;<,>.?!@#$%'
75-
76-
symbols = lower + upper + digits + punct
77-
78-
if 8 <= lenght <= 74:
79-
pass
80-
elif lenght > 74:
81-
return await ctx.send("Password must be contain 74 symbols or fewer.")
82-
else:
83-
return await ctx.send("Password must be contain 8 symbols or higher.")
84-
85-
password = ''.join(random.sample(symbols, lenght))
74+
await interaction.send(embed=slotmachine)
75+
76+
@nextcord.slash_command()
77+
async def password(self, interaction, length: int = nextcord.SlashOption(min_value=8,
78+
max_value=74,
79+
default=8)):
80+
"""
81+
Random password generator.
82+
"""
83+
password = ''.join(
84+
random.sample(string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation,
85+
length))
8686
embed = nextcord.Embed(
8787
title='Password generator',
88-
description=f'Your random password: ``{password}``',
89-
timestamp=ctx.message.created_at,
88+
description=f'Your random generated password: ``{password}``',
89+
timestamp=interaction.created_at,
9090
colour=0x45fc03
9191
)
92-
embed.add_field(name='Warning ⚠️', value='Password generator not recommended for use on guilds.')
93-
await ctx.send(embed=embed)
92+
await interaction.send(embed=embed, ephemeral=True)
9493

95-
@commands.command()
96-
async def coin(self, ctx):
94+
@nextcord.slash_command()
95+
async def coin(self, interaction):
96+
"""
97+
Heads or tails.
98+
"""
9799
array_coins = ['heads', 'tails']
98100
coin_flip = random.choice(array_coins)
99101

100102
embed = nextcord.Embed(description=f"Coin tossed and **{coin_flip}** falls out.", colour=0xdf03fc)
101-
await ctx.send(embed=embed)
103+
await interaction.send(embed=embed)
102104

103-
@commands.command()
104-
async def say(self, ctx, *, sentence):
105+
@nextcord.slash_command()
106+
async def say(self, interaction, sentence):
107+
"""
108+
Say message as the bot.
109+
"""
105110
embed = nextcord.Embed(
106111
description=f'{sentence}',
107112
)
108-
await ctx.send(embed=embed)
109-
await ctx.message.delete()
110-
111-
@commands.command()
112-
async def reverse(self, ctx, *, sentence):
113-
await ctx.send(f"{sentence[::-1]}")
114-
115-
@commands.command()
116-
async def random(self, ctx, first_num: int = 1, *, second_num: int = 10):
117-
try:
118-
embed = nextcord.Embed(description=f"Number in range **from {first_num} to {second_num}**")
119-
embed.add_field(name="Result", value=f"{random.randint(first_num, second_num)}")
120-
await ctx.send(embed=embed)
121-
122-
except Exception as error:
123-
print(error)
124-
125-
@commands.command()
126-
async def guess(self, ctx):
127-
128-
await ctx.reply("Type the number from 1 to 5")
129-
130-
try:
131-
number = await self.client.wait_for('message', check=lambda message: message.author == ctx.author,
132-
timeout=7)
133-
h_number = random.randint(1, 5)
134-
135-
except asyncio.TimeoutError:
136-
await ctx.send("Time is out... Try again.")
137-
138-
else:
139-
try:
140-
if int(number.content) == h_number and 1 <= int(number.content) <= 5:
141-
color = nextcord.Color.green()
142-
result = f"**YOU RIGHT!**\n\nYour number: {number.content}\nHidden number: {h_number}"
143-
144-
elif int(number.content) != h_number and 1 <= int(number.content) <= 5:
145-
color = nextcord.Color.red()
146-
result = f"**YOU NOT RIGHT..**\n\n**Your number:** {number.content}\n**Hidden number:** {h_number}"
147-
148-
else:
149-
color = nextcord.Color.yellow()
150-
result = "Ooops! Error happens..."
151-
152-
embed = nextcord.Embed(title="Guees the number", description=result, colour=color)
153-
await ctx.send(embed=embed)
154-
155-
except ValueError:
156-
await ctx.send("I don`t get which you typed...")
157-
158-
@commands.command()
159-
async def weather(self, ctx, *, a_city):
113+
await interaction.send(embed=embed)
114+
await interaction.message.delete()
115+
116+
@nextcord.slash_command()
117+
async def reverse(self, interaction, sentence):
118+
"""
119+
Reverse entered message.
120+
"""
121+
await interaction.send(f"{sentence[::-1]}")
122+
123+
@nextcord.slash_command()
124+
async def random(self, interaction,
125+
first_num: int = nextcord.SlashOption(name="minimum",
126+
description="Minimum number of the range "
127+
"(0 if not specified)",
128+
min_value=0, default=0),
129+
second_num: int = nextcord.SlashOption(name="maximum",
130+
description="Maximum number of the range "
131+
"(100 if not specified)",
132+
min_value=0, default=100)):
133+
"""
134+
Random number within the specified range.
135+
"""
136+
embed = nextcord.Embed(description=f"Random number in the range **from {first_num} to {second_num}**")
137+
embed.add_field(name="Result", value=f"{random.randint(first_num, second_num)}")
138+
await interaction.send(embed=embed)
139+
140+
@is_weather_active()
141+
@nextcord.slash_command()
142+
async def weather(self, interaction, _city):
143+
"""
144+
Get a weather forecast in certain city.
145+
"""
160146
try:
161147
ow_token = config["bot"]["openweather_token"]
162148
response = requests.get(
163-
f'http://api.openweathermap.org/data/2.5/weather?q={a_city}&lang=ru&units=metric&appid={ow_token}')
149+
f'http://api.openweathermap.org/data/2.5/weather?q={_city}&lang=ru&units=metric&appid={ow_token}')
164150
data = response.json()
165151

166152
city = data["name"]
@@ -203,11 +189,11 @@ async def weather(self, ctx, *, a_city):
203189
embed.add_field(name="Sunrise", value=f"{sunrise_timestamp}")
204190
embed.add_field(name="Sunset", value=f"{sunset_timestamp}")
205191

206-
await ctx.send(embed=embed)
192+
await interaction.send(embed=embed)
207193

208194
except Exception as err:
209195
print(err)
210-
await ctx.send("Incorrect city.")
196+
await interaction.send("Incorrect city.")
211197

212198

213199
def setup(client):

0 commit comments

Comments
 (0)