-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
71 lines (46 loc) · 1.81 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
import discord
from discord.ext import commands
from sqlalchemy import create_engine, text
import threading
import time
from sqlalchemy.exc import SQLAlchemyError, OperationalError
backup = create_engine('sqlite:///urls_old.db')
engine = create_engine('sqlite:///urls.db')
def getRandomFile():
try:
with engine.connect() as connection:
result = connection.execute(text("SELECT * FROM urls ORDER BY RANDOM() LIMIT 1")).fetchall()
return result[0]
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("failed to connect to the main urls.db using urls_old.db")
return getBackupRandomFile();
def getBackupRandomFile():
with backup.connect() as connection:
result = connection.execute(text("SELECT * FROM urls ORDER BY RANDOM() LIMIT 1")).fetchall()
return result[0]
random_file = None
def update_random_file():
global random_file
while True:
random_file = getRandomFile() # Generate a random float
time.sleep(2.5) # Wait for 5 seconds
threading.Thread(target=update_random_file).start()
bot = discord.Bot()
@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")
@bot.slash_command(description = "get random chan video or image hehe")
@commands.is_nsfw()
async def randomfile(ctx):
rand_file = random_file
response_str = f''
response_str = f'{rand_file[3]}'
response_str += f'\n site: {rand_file[1]}'
response_str += f'\n board: {rand_file[2]}'
response_str += f'\n comment: {rand_file[4]}'
await ctx.respond(response_str) # Send the embed with some text
@randomfile.error
async def on_application_command_error(ctx: discord.ApplicationContext, error: discord.DiscordException):
await ctx.respond("Need to be in nsfw channel!!!")
bot.run('discord key')