-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordBot.py
81 lines (65 loc) · 2.79 KB
/
DiscordBot.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
import glob
import os
import platform
import sys
import discord
import pandas as pd
from discord.ext import commands
from autobooks.AutoBooks import run, version, script_dir, config, csv_path, LOG_FILENAME, logger
# Bot Settings
token = config["discord_bot_token"]
bot = commands.Bot(command_prefix='?')
@bot.event
async def on_ready():
logger.info(f'{bot.user} has connected to Discord!')
@bot.command(name='autobooks')
async def web(ctx):
# Send starting embed and running web
embed_start = discord.Embed(title="Running AutoBooks. This may take awhile....",
description=f"V.{version} \n Logfile: {LOG_FILENAME}", color=0xFFAFCC)
embed_start.set_image(url="https://raw.githubusercontent.com/ivybowman/AutoBooks/main/img/logo/small_pink.png")
embed_start.set_footer(text="OS: " + platform.platform() + " Host: " + platform.node())
await ctx.channel.send(embed=embed_start)
web_info = ["test-data", "test-data2"]
run()
# Ending Embed
embed_end = discord.Embed(title="AutoBooks Web Finished",
description="See log info below for details. ErrorCount: " + str(web_info[1]),
color=0xFFAFCC)
embed_end.set_thumbnail(url="https://raw.githubusercontent.com/ivybowman/AutoBooks/main/img/icon_pink.png")
if web_info[0] != "":
embed_end.add_field(name="Book List", value=str(web_info[0]), inline=False)
await ctx.channel.send(embed=embed_end)
# Logfile fetching
files = glob.glob(os.path.join(script_dir, "log", "*.log"))
files2 = sorted(files, key=os.path.getmtime, reverse=True)
print(files2[0])
await ctx.channel.send(file=discord.File(files2[0]))
@bot.command(name='log')
async def hello(ctx):
files = glob.glob(os.path.join(script_dir, "log", "*.log"))
max_file = max(files, key=os.path.getmtime)
print(max_file)
await ctx.channel.send("Fetched latest AutoBooks logfile: \n" + max_file)
await ctx.channel.send(file=discord.File(max_file))
@bot.command(name='csv')
async def hello(ctx):
try:
df = pd.read_csv(csv_path, sep=",")
embed_var = discord.Embed(title="Autobooks Known Books",
description=df['book_title'].to_string(index=False), color=0xFFAFCC)
embed_var.set_footer(text="OS: " + platform.platform() + " Host: " + platform.node())
await ctx.channel.send(embed=embed_var)
except FileNotFoundError:
await ctx.channel.send("Known Books CSV not found.")
# await ctx.channel.send(file=discord.File(max_file))
def discord_run():
if token == "":
logger.critical("Bot token not found in config file, exiting.")
else:
bot.run(token)
if __name__ == "__main__":
try:
discord_run()
except KeyboardInterrupt:
sys.exit(1)