-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.py
287 lines (227 loc) · 10.6 KB
/
Bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import re
import csv
import json
import aiohttp
import asyncio
import datetime
import requests
import discord
from typing import List
from discord.ext import commands, tasks
bot = discord.Bot()
# Main BOT Events
@bot.event
async def on_ready():
print(f'======================= ')
print(f'STATUS : ONLINE')
print(f'======================= ')
print(f'BOT : {bot.user}')
await bot.change_presence(activity=discord.Game(name="With Stars"))
start_time = datetime.datetime.now()
bot.start_time = start_time
@bot.slash_command(name='ping', description="Sends Bot's Latency")
async def ping(ctx):
latency = bot.latency * 1000
uptime = datetime.datetime.now() - bot.start_time
uptime_seconds = uptime.total_seconds()
uptime_str = str(datetime.timedelta(seconds=uptime_seconds)).split(".")[0]
num_servers = len(bot.guilds)
embed = discord.Embed(
title="_*Pong !*_",
color=0x2f3136
)
embed.add_field(name="Uptime", value=uptime_str, inline=False)
embed.add_field(name="Latency", value=f"{latency:.2f}ms", inline=False)
embed.add_field(name="Servers", value=num_servers, inline=False)
await ctx.respond(embed=embed)
@bot.slash_command(name='botinfo', description="Sends Bot's Information")
async def botinfo(ctx):
embed = discord.Embed(
title="Bot Information",
description="Information About The Bot",
color=0x2f3136
)
embed.add_field(name="Bot Name", value=bot.user, inline=False)
embed.add_field(name="Bot Owner", value="SpreadSheets600", inline=False)
embed.add_field(name="Bot Created At", value=bot.user.created_at.strftime("%Y-%m-%d %H:%M:%S"), inline=False)
embed.add_field(name="\u200b", value="\u200b", inline=False)
embed.add_field(name="Github", value="[Click Here](https://github.com/SpreadSheets600)")
await ctx.respond(embed=embed, view=Invite())
class Invite(discord.ui.View):
def __init__(self):
super().__init__(timeout=30)
button = discord.ui.Button(
label='Invite Me',
style=discord.ButtonStyle.url,
url='https://discord.com/oauth2/authorize?client_id=1111555652612018246&permissions=8&scope=bot'
)
self.add_item(button)
@bot.slash_command()
async def meaning(ctx, word):
url = f'https://api.dictionaryapi.dev/api/v2/entries/en/{word}'
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data:
first_meaning = data[0]['meanings'][0]
embed = discord.Embed(title=f"Meaning Of {word.capitalize()}", color=discord.Color.blue())
embed.add_field(name="Part of Speech", value=first_meaning['partOfSpeech'].capitalize())
embed.add_field(name="Definition", value=first_meaning['definitions'][0]['definition'], inline=False)
if 'synonyms' in first_meaning:
embed.add_field(name="Synonyms", value=", ".join(first_meaning['synonyms']).capitalize(), inline=False)
if 'example' in first_meaning:
embed.add_field(name="Example", value=first_meaning['example'], inline=False)
other_meanings = data[0]['meanings'][1:]
if other_meanings:
embed.set_footer(text="Click the button for more meanings")
view = MeaningView(ctx, other_meanings, word)
await ctx.respond(embed=embed, view=view)
else:
await ctx.respond(embed=embed)
else:
await ctx.respond(f"No Meaning Found For {word}")
else:
await ctx.respond("Failed To Fetch Meaning")
class MeaningView(discord.ui.View):
def __init__(self, ctx, meanings, word):
super().__init__()
self.ctx = ctx
self.word = word
self.meanings = meanings
self.current_meaning_index = 0
@discord.ui.button(label="Next Meaning", style=discord.ButtonStyle.secondary, custom_id="next_meaning")
async def button_callback(self, button, interaction):
self.current_meaning_index = (self.current_meaning_index + 1) % len(self.meanings)
await self.show_current_meaning()
async def show_current_meaning(self):
word = self.word.capitalize()
meaning = self.meanings[self.current_meaning_index]
embed = discord.Embed(title=f"Meaning Of {word} ({self.current_meaning_index + 1}/{len(self.meanings)})", color=discord.Color.blue())
embed.add_field(name="Part Of Speech", value=meaning['partOfSpeech'].capitalize())
embed.add_field(name="Definition", value=meaning['definitions'][0]['definition'], inline=False)
if 'synonyms' in meaning:
embed.add_field(name="Synonyms", value=", ".join(meaning['synonyms']).capitalize(), inline=False)
if 'example' in meaning:
embed.add_field(name="Example", value=meaning['example'], inline=False)
embed.set_footer(text=f"Page {self.current_meaning_index + 1}/{len(self.meanings)}")
await self.ctx.respond(embed=embed)
class TicTacToeButton(discord.ui.Button["TicTacToe"]):
def __init__(self, x: int, y: int):
super().__init__(style=discord.ButtonStyle.secondary, label="\u200b", row=y + 1)
self.x = x
self.y = y
async def callback(self, interaction: discord.Interaction):
assert self.view is not None
view: TicTacToe = self.view
state = view.board[self.y][self.x]
if state in (view.X, view.O):
return await interaction.response.send_message("This Position Is Already Occupied!", ephemeral=True)
current_player_id = view.players[view.current_player_index].id
if interaction.user.id != current_player_id:
return await interaction.response.send_message("It's Not Your Turn!", ephemeral=True)
if view.current_player == view.X:
self.style = discord.ButtonStyle.danger
self.label = "X"
view.board[self.y][self.x] = view.X
o = view.players[1]
content = f"It Is Now {o.mention}'s Turn"
view.current_player_index = (view.current_player_index + 1) % 2
view.current_player = view.O
else:
self.style = discord.ButtonStyle.success
self.label = "O"
view.board[self.y][self.x] = view.O
x = view.players[0]
content = f"It Is Now {x.mention} Turn"
view.current_player_index = (view.current_player_index + 1) % 2
view.current_player = view.X
self.disabled = True
winner = view.check_board_winner()
if winner is not None:
if winner == view.X:
x = view.players[0]
content = f"{x.mention} Won ! Congratulations !"
elif winner == view.O:
o = view.players[1]
content = f"{o.mention} Won ! Congratulations !"
else:
content = "Damnn ! It's A Tie !"
for child in view.children:
child.disabled = True
view.stop()
await interaction.response.edit_message(content=content, view=view)
class TicTacToe(discord.ui.View):
X = -1
O = 1
Tie = 2
def __init__(self, challenger: discord.Member, opponent: discord.Member):
super().__init__()
self.players = [challenger, opponent]
self.current_player_index = 0
self.current_player = self.X
self.board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
for x in range(3):
for y in range(3):
self.add_item(TicTacToeButton(x, y))
def check_board_winner(self):
for row in self.board:
if row.count(row[0]) == len(row) and row[0] != 0:
return row[0]
for col in range(len(self.board)):
if self.board[0][col] == self.board[1][col] == self.board[2][col] and self.board[0][col] != 0:
return self.board[0][col]
if self.board[0][0] == self.board[1][1] == self.board[2][2] and self.board[0][0] != 0:
return self.board[0][0]
if self.board[0][2] == self.board[1][1] == self.board[2][0] and self.board[0][2] != 0:
return self.board[0][2]
for row in self.board:
if 0 in row:
return None
return self.Tie
class ChallengeView(discord.ui.View):
def __init__(self, challenger: discord.Member, opponent: discord.Member):
super().__init__()
self.challenger = challenger
self.opponent = opponent
async def on_timeout(self):
for item in self.children:
item.disabled = True
@discord.ui.button(label='Accept', style=discord.ButtonStyle.success)
async def accept_button(self, button: discord.ui.Button, interaction: discord.Interaction):
await interaction.response.send_message("Challenge Accepted !", ephemeral=True)
game = TicTacToe(self.challenger, self.opponent)
await interaction.message.edit(content=f"Tic Tac Toe: {self.challenger.mention} Goes First !", view=game)
@discord.ui.button(label='Decline', style=discord.ButtonStyle.danger)
async def decline_button(self, button: discord.ui.Button, interaction: discord.Interaction):
await interaction.response.send_message("Challenge Declined !", ephemeral=True)
self.stop()
@bot.slash_command(name="tictactoe", description="Challenge Another Player To Play Tic Tac Toe")
async def tictactoe(ctx: commands.Context, opponent: discord.Member):
challenger = ctx.author
if opponent == ctx.author:
return await ctx.respond("You Cannot Challenge Yourself!")
await ctx.respond(f"Challenging {opponent.mention} To Play Tic Tac Toe!", ephemeral=True)
challenge_message = await ctx.send(f"{opponent.mention}, You Have Been Challenged By {challenger.mention} To Play Tic Tac Toe!", view=ChallengeView(challenger, opponent))
def check(reaction, user):
return user == opponent and str(reaction.emoji) in ["✅", "❌"] and reaction.message.id == challenge_message.id
game = TicTacToe(challenger, opponent)
await challenge_message.edit(content=f"Tic Tac Toe: {challenger.mention} goes first!", view=game)
bot.load_extension('Cogs.AI')
bot.load_extension('Cogs.Fun')
bot.load_extension('Cogs.AFK')
bot.load_extension('Cogs.Help')
bot.load_extension('Cogs.Anime')
bot.load_extension('Cogs.Utils')
bot.load_extension('Cogs.Movie')
bot.load_extension('Cogs.Emotes')
bot.load_extension('Cogs.Reminder')
bot.load_extension('Cogs.Translate')
bot.load_extension('Cogs.Moderation')
bot.load_extension('Cogs.Information')
bot.load_extension('Cogs.Entertainment')
bot.run('TOKEN')