-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
84 lines (58 loc) · 2.35 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
from nextcord.ext.commands import Bot, Context
from nextcord import Intents, TextChannel
from contextlib import ExitStack
from json_db import JSON_DB
from updater import start
intents = Intents.default()
intents.messages = True
class RSSBot(Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.updates_scheduled = False
client = RSSBot(intents=intents, command_prefix="rss!")
@client.listen()
async def on_ready():
if not client.updates_scheduled:
start(client.loop, db, client)
print("We're alive!!!")
@client.command()
async def add(ctx: Context, feed_url: str):
guilds = db.get("feeds", default={})
feeds = guilds.get(str(ctx.guild.id), [])
feeds.append(feed_url)
guilds[str(ctx.guild.id)] = feeds
db.set("feeds", guilds)
await ctx.send(f"Added `{feed_url}` to the {ctx.guild.name} feed list")
@client.command()
async def remove(ctx: Context, feed_url: str):
guilds = db.get("feeds", default={})
feeds = guilds.get(str(ctx.guild.id), [])
try:
feeds.remove(feed_url)
except ValueError:
await ctx.send(f"`{feed_url}` was already removed from the {ctx.guild.name} feed list")
else:
guilds[str(ctx.guild.id)] = feeds
db.set("feeds", guilds)
await ctx.send(f"Removed `{feed_url}` to the {ctx.guild.name} feed list")
@client.command(name="list")
async def list_command(ctx: Context):
guilds = db.get("feeds", default={})
feeds = guilds.get(str(ctx.guild.id), [])
feed_list = "\n".join(feeds) if feeds else "*No Feeds Found*"
await ctx.send(f"**Feed List**\n{feed_list}")
@client.command()
async def setup(ctx: Context, channel: TextChannel):
guilds = db.get("guilds", default={})
guilds[str(channel.guild.id)] = channel.id
db.set("guilds", guilds)
await ctx.send(f"Successfully set {channel.mention} as the RSS feed channel")
@client.command()
async def info(ctx: Context):
channel_id = db.get("guilds", default={}).get(str(ctx.guild.id), None)
channel = ctx.guild.get_channel(channel_id).mention if channel_id is not None else "*Not Set*"
await ctx.send(f"The RSS feed channel for {ctx.guild.name} is {channel}")
with ExitStack() as stack:
stack.enter_context(token_file := open("token.secret"))
stack.enter_context(db := JSON_DB(__file__))
client.run(token_file.read().strip())