-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
309 lines (290 loc) · 13.2 KB
/
main.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Imports
import os
import time
import os.path
import discord
import datetime
import requests
import json
from discord import option, ApplicationContext
from discord.ext import commands
from discord.ext.commands import *
from discord.ext import tasks
from keep_alive import keep_alive
# Startup and Variables
ids = [
816941773032390676,
738290097170153472,
705462972415213588,
706697300872921088
]
console = False
log = True
if os.name == 'nt': os.system('cls')
else: os.system('clear')
print('Checking for rate limit errors...')
r = requests.head(url="https://discord.com/api/v1")
try:
print(f"Rate limit error found: {int(r.headers['Retry-After']) / 60} minutes left")
raise(SystemExit)
except: print("No rate limit found.")
time.sleep(1)
intents = discord.Intents.all()
errHandlerVer = 'v2.4'
botVer = 'v1.0'
if os.name == 'nt': os.system('cls')
else: os.system('clear')
owner = '@notsniped'
homedir = os.path.expanduser("~")
client = discord.Bot()
global startTime
startTime = time.time()
client.remove_command('help')
theme_color = 0xffbd59
color_success = 0x77b255
color_fail = 0xc92424
rootdir = 'C://Users//dhruvbhat//OneDrive//Desktop//increment.io'
loggerHandler_path = 'botLog/log.txt'
errorHandler_path = 'botLog/errors.txt'
correctnumber = {}
class colors:
cyan = '\033[96m'
red = '\033[91m'
green = '\033[92m'
end = '\033[0m'
with open('database/count.json', 'r') as f: count = json.load(f)
with open('database/configuration/countchannel.json', 'r') as f: countchannel = json.load(f)
with open('database/configuration/warning.json', 'r') as f: warnmsg = json.load(f)
with open('database/configuration/autoreactions.json', 'r') as f: autoreactions = json.load(f)
def savedata():
with open('database/count.json', 'w+') as f: json.dump(count, f)
with open('database/configuration/countchannel.json', 'w+') as f: json.dump(countchannel, f)
with open('database/configuration/autoreactions.json', 'w+') as f: json.dump(autoreactions, f)
def get_time():
now = datetime.datetime.now()
return now.strftime("%H:%M:%S")
# Events
@client.event
async def on_ready():
if os.name == 'nt': os.system('cls')
else: os.system('clear')
await client.change_presence(
activity=discord.Activity(
type=discord.ActivityType.playing,
name=f"the epic comeback. (+help) | {str(len(client.guilds))} guilds"
)
)
print('Bot is online')
print('==================')
print('------------------')
print('Bot Info')
print(f'Bot version: {colors.cyan}{botVer}{colors.end}')
print(f'Error handler version: {colors.cyan}{errHandlerVer}{colors.end}')
print(f"Username: {colors.green}{client.user.name}{colors.end}")
print(f"Bot id: {colors.green}{client.user.id}{colors.end}")
print(f"Developer name: {colors.green}{owner}{colors.end}")
print('==================')
print('Bot config:')
print('------------------')
print(f'Ping: {round(client.latency * 1000)}')
print('------------------')
boot = str(datetime.timedelta(seconds=int(round(time.time()-startTime))))
print(f'Startup time: {boot}')
print('------------------')
print(f'Server count: {str(len(client.guilds))}')
print('------------------')
if log: print(f'Logging: {colors.green}{log}{colors.end}')
else: print(f'Logging: {colors.red}{log}{colors.end}')
print('==================')
print('Bot admins')
print('------------------')
print(colors.cyan)
for id in ids: print(id)
print(colors.end)
print('==================')
print('System info')
print('Running as: ' + str(os.system("whoami")))
print('------------------')
print('Os name: ' + str(os.name))
print('------------------')
print('Current working dir: ' + str(os.getcwd()))
print(f'System directory: {homedir}')
print('------------------')
# Error Handler
@client.event
async def on_command_error(ctx, error):
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
if isinstance(error, discord.ext.commands.CommandNotFound):
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at CommandNotFound. Details: This command does not exist.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at CommandNotFound. Details: This command does not exist.{colors.end}')
else: pass
if isinstance(error, discord.ext.commands.CommandOnCooldown):
await ctx.send(f':warning: This command is currently on cooldown, try after **{str(datetime.timedelta(seconds=int(round(error.retry_after))))}**', delete_after=5)
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at CommandOnCooldown. Details: This command is currently on cooldown.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at CommandOnCooldown. Details: This command is currently on cooldown.{colors.end}')
else: pass
if isinstance(error, discord.ext.commands.MissingRequiredArgument):
await ctx.send(':x: Your command has missing required argument(s).', delete_after=3)
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at MissingRequiredArgument. Details: The command can\'t be executed because required arguments are missing.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at MissingRequiredArgument. Details: The command can\'t be executed because required arguments are missing.{colors.end}')
else: pass
if isinstance(error, discord.ext.commands.MissingPermissions):
await ctx.send(':x: You don\'t have permissions to use this command.', delete_after=3)
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at MissingPermissions. Details: The user doesn\'t have the required permissions.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at MissingPermissions. Details: The user doesn\'t have the required permissions.{colors.end}')
else: pass
if isinstance(error, discord.ext.commands.BadArgument):
await ctx.send(':x: Invalid argument.', delete_after=3)
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at BadArgument.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at BadArgument.{colors.end}')
else: pass
if isinstance(error, discord.ext.commands.BotMissingPermissions):
await ctx.send(':x: I don\'t have permissions to do this. Kindly manage my role permissions to get this feature working.')
if os.name == 'nt':
with open(errorHandler_path, 'a') as f:
f.write(f'[{current_time}/WARN] Ignoring exception at BotMissingPermissions.\n Details: The bot doesn\'t have the required permissions.\n')
f.close()
print(f'{colors.red}[{current_time}/WARN] Ignoring exception at BotMissingPremissions. Details: The bot doesn\'t have the required permissions.{colors.end}')
else: pass
snipe_message_author = {}
snipe_message_content = {}
@client.event
async def on_message(message):
global count
if str(message.channel.id) not in count:
count[str(message.channel.id)] = 1
savedata()
if str(message.guild.id) not in countchannel:
countchannel[str(message.guild.id)] = 0
savedata()
if str(message.guild.id) not in warnmsg:
warnmsg[str(message.guild.id)] = 0
savedata()
if str(message.guild.id) not in autoreactions:
autoreactions[str(message.guild.id)] = 1
savedata()
if not message.author.bot:
if message.channel.id == countchannel[str(message.guild.id)]:
try:
if int(message.content) == count[str(message.channel.id)]:
count[str(message.channel.id)] += 1
if autoreactions[str(message.guild.id)] == 1: await message.add_reaction('☑')
savedata()
else:
if autoreactions[str(message.guild.id)] == 1: await message.add_reaction('❌')
await message.reply(f'**Wrong!** The next number is `{count[str(message.channel.id)]}`', mention_author = False, delete_after = 3)
except:
if autoreactions[str(message.guild.id)] == 1: await message.add_reaction('⚠')
await client.process_commands(message)
# Commands
@client.slash_command(
name="uptime",
description="See the bot's current session uptime."
)
async def uptime(ctx: ApplicationContext):
uptime = str(datetime.timedelta(seconds=int(round(time.time()-startTime))))
await ctx.send(f'This session has been running for **{uptime}**')
@client.slash_command(
name="help",
description="Need help?"
)
async def help(ctx: ApplicationContext):
emb = discord.Embed(title=f'increment.io Commands', description=f'I am a counting bot who looks for numbers and makes sure that the count doesn\'t get messed up. If you want help on commands or want a more organized view, check https://notsniped.github.io/increment.io/commands\n :warning: This bot is still WIP. Some commands/features may not work as expected.\n\n**Prefix:** `/`\n**Information:** ```/help, /ping, /stats, /serverstats, /credits```\n**Counting:** ```/setchannel, /numberonly [on/off], /reactions [on/off], /setnumber [number], /resetcount```', color=theme_color)
await ctx.send(embed = emb)
@client.slash_command(
name="ping",
description="View the bot latency (in ms)"
)
async def ping(ctx: ApplicationContext):
await ctx.send(f':ping_pong: Pong! In **{round(client.latency * 1000)}ms**.')
@client.slash_command(
name="credits",
description="View the people behind this bot's development."
)
async def credits(ctx: ApplicationContext):
emb = discord.Embed(title='Bot Credits', description='Owner: <@!738290097170153472>\nHelpers: <@!706697300872921088>, <@!705462972415213588>',color=theme_color)
emb.set_footer(text='increment.io >> https://notsniped.github.io/increment.io')
await ctx.send(embed=emb)
@client.slash_command(
name="serverstats",
description="View counting stats for this server."
)
async def serverstats(ctx: ApplicationContext):
servericon = ctx.guild.icon_url
setchannelstats = countchannel[str(ctx.guild.id)]
setchannelmaxcount = count[setchannelstats]
emb12 = discord.Embed(title='This Server\'s Stats', color=theme_color)
emb12.add_field(name='Count Channel', value=f'<#{setchannelstats}>')
emb12.add_field(name='Current Count', value=setchannelmaxcount)
emb12.set_thumbnail(url=servericon)
await ctx.send(embed = emb12)
# Count Commands
@client.slash_command(
name="setchannel",
description="Sets this channel as the server's counting channel."
)
@commands.has_permissions(administrator = True)
async def setchannel(ctx: ApplicationContext):
try:
countchannel[str(ctx.guild.id)] = ctx.channel.id
savedata()
await ctx.send(f':white_check_mark: <#{channel_to_set}> set as counting channel.')
except: await ctx.send(':x: Unable to set count channel. Try again later.')
@client.slash_command(
name="reactions",
description="Choose whether you want bot reactions enabled or not."
)
@option(name="value", description="Choose a setting value", type=str, choices=["on", "off"])
@commands.has_permissions(administrator = True)
async def reactions(ctx: ApplicationContext, value: str):
if value == 'on':
if autoreactions[str(ctx.guild.id)] == 1: await ctx.send(':warning: This feature is already enabled.')
else:
autoreactions[str(ctx.guild.id)] = 1
savedata()
await ctx.send(f':white_check_mark: Turned **on** count reactions.')
elif value == 'off':
if autoreactions[str(ctx.guild.id)] == 0: await ctx.send(':warning: This feature is already disabled')
else:
autoreactions[str(ctx.guild.id)] = 0
savedata()
await ctx.send(f':white_check_mark: Turned **off** count reactions.')
else: await ctx.send(f'\'{setting}\' is not a valid option. You can choose between `on` and `off`')
@client.slash_command(
name="setnumber",
description="Set the current count to a new value."
)
@option(name="new_count", description="What do you want to set the count to?", type=int)
async def setnumber(ctx: ApplicationContext, new_count: int):
if new_count < 1: raise(discord.ext.commands.BadArgument)
else:
count[str(ctx.channel.id)] = new_count
savedata()
await ctx.reply(f':white_check_mark: Count set to `{count[str(ctx.channel.id)]}`')
@client.slash_command(
name="resetcount",
description="Reset the current count."
)
async def resetcount(ctx):
count[str(ctx.channel.id)] = 1
savedata()
await ctx.reply(':white_check_mark: Count successfully reset back to `1`')
# Client initialization
keep_alive()
client.run('') # Insert your bot token here