1
- import asyncio
2
-
3
1
import nextcord
4
2
from nextcord .ext import commands
5
- import random
6
3
4
+ import random
5
+ import string
7
6
import requests
8
7
import datetime
9
8
from datetime import timezone , timedelta
10
9
import math
11
10
import configparser
12
11
12
+ from utils .decorators import is_weather_active
13
+
13
14
config = configparser .ConfigParser ()
14
15
config .read ("config.ini" )
15
16
@@ -18,10 +19,13 @@ class Fun(commands.Cog):
18
19
def __init__ (self , client ):
19
20
self .client = client
20
21
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
+ """
23
27
player_dice , bot_dice = [random .randint (1 , 6 ) for _ in range (2 )]
24
- print ( player_dice , bot_dice )
28
+
25
29
if player_dice > bot_dice :
26
30
result = "You won!"
27
31
color = nextcord .Color .green ()
@@ -40,10 +44,13 @@ async def dice(self, ctx):
40
44
f"{ result } " ]),
41
45
colour = color )
42
46
43
- await ctx .send (embed = embed )
47
+ await interaction .send (embed = embed )
44
48
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
+ """
47
54
emojis = ["🍎" , "🍊" , "🍐" , "🍋" , "🍉" , "🍇" , "🍓" , "🍒" , "🔔" , "💎" , ":seven:" ]
48
55
49
56
a = random .choice (emojis )
@@ -64,103 +71,82 @@ async def roll(self, ctx):
64
71
65
72
slotmachine = nextcord .Embed (title = "Slots" , description = f"🎰 ({ a } |{ b } |{ c } )" , colour = color )
66
73
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 ))
86
86
embed = nextcord .Embed (
87
87
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 ,
90
90
colour = 0x45fc03
91
91
)
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 )
94
93
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
+ """
97
99
array_coins = ['heads' , 'tails' ]
98
100
coin_flip = random .choice (array_coins )
99
101
100
102
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 )
102
104
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
+ """
105
110
embed = nextcord .Embed (
106
111
description = f'{ sentence } ' ,
107
112
)
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 \n Your number: { number .content } \n Hidden 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
+ """
160
146
try :
161
147
ow_token = config ["bot" ]["openweather_token" ]
162
148
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 } ' )
164
150
data = response .json ()
165
151
166
152
city = data ["name" ]
@@ -203,11 +189,11 @@ async def weather(self, ctx, *, a_city):
203
189
embed .add_field (name = "Sunrise" , value = f"{ sunrise_timestamp } " )
204
190
embed .add_field (name = "Sunset" , value = f"{ sunset_timestamp } " )
205
191
206
- await ctx .send (embed = embed )
192
+ await interaction .send (embed = embed )
207
193
208
194
except Exception as err :
209
195
print (err )
210
- await ctx .send ("Incorrect city." )
196
+ await interaction .send ("Incorrect city." )
211
197
212
198
213
199
def setup (client ):
0 commit comments