This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
291 lines (222 loc) · 9 KB
/
main.py
File metadata and controls
291 lines (222 loc) · 9 KB
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
import os
import discord
import requests
from discord.ext import commands
from cast_utils import get_info, get_links, rcol, ses
from keep_alive import keep_alive
# Tracks the currently playing episode
CURRENT = None
# Bot Setup
bot = commands.Bot(command_prefix="?", help_command=None)
@bot.event
async def on_ready():
print(f"{bot.user.name} is ready to jam now.")
# Handling wrong commands & missing permissions
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, discord.ext.commands.errors.CommandNotFound):
await ctx.send(
"_I do not understand that command! Try `?help` to get a list of commands._",
delete_after=120)
if isinstance(error, commands.MissingPermissions):
await ctx.send("`You are not an admin!`", delete_after=120)
@bot.command(name="announce", aliases=['a', '<'])
@commands.has_role("admin")
async def announce_(ctx):
"""
Announces the release of a new podcast
"""
await ctx.trigger_typing()
if get_info():
pod_num, pod_title, pod_published, pod_shortdesc = get_info()
# Preparing embed
file = discord.File("./castbot.png")
promo_url = rf"https://gitlab.com/nisercast/nisercast.gitlab.io/-/raw/master/assets/episodes/posters/{str(pod_num - 1).zfill(3)}.png"
embed = discord.Embed(
title=pod_title,
description=
f"_Publication Timestamp: {pod_published}_\n\n{pod_shortdesc}\n\n\u200b",
colour=discord.Color.from_rgb(*rcol()))
links = get_links()
links_final = [
f"[Google Podcast]({links[0]})\n",
f"[Spotify Podcast]({links[1]})\n",
f"[Apple Podcast]({links[2]})\n\n",
"_You can also listen to episodes via this bot. Type `?help` to learn more._"
]
embed.set_image(url=promo_url)
embed.add_field(name="Podcast Links:",
value=f"{''.join(x for x in links_final)}",
inline=False)
embed.add_field(name="\u200b", value="\u200b", inline=False)
embed.set_footer(
text=
"Custom Podcast Bot for NiSERCast Discord Server | Fork me on GitHub (https://github.com/sdgniser/castbot)",
icon_url="attachment://castbot.png")
await ctx.channel.send(content="@everyone", file=file, embed=embed)
await ctx.message.delete()
else:
return await ctx.channel.send("`Podcast not found!`", delete_after=120)
@bot.command(name="play", aliases=['p'])
async def play_(ctx, *, podcast=1):
"""
Plays the requested NiSERCast episode, sourced from GitLab
podcast: int
Episode number
Defaults to ``1`` (Episode 1)
"""
global CURRENT
# Discord limiation: cannot typecheck here
if podcast == CURRENT:
return await ctx.send(
f"`Episode {podcast} is already being played! Try ?r to resume an episode.`",
delete_after=60)
source = rf"https://gitlab.com/nisercast/nisercast.gitlab.io/-/raw/master/assets/episodes/{str(podcast).zfill(3)}.mp3"
await ctx.trigger_typing()
# Checking, if file exists on GitLab
resp = ses.get(source).status_code
if resp != 200:
return await ctx.send(
f"`Episode {podcast} either does not exist or GitLab is experiencing issues at this time!`",
delete_after=120)
author = ctx.author
if hasattr(author, "voice") and author.voice and author.voice.channel:
voice_channel = author.voice.channel
voice = ctx.channel.guild.voice_client
# Player loop to skip / change episode
if voice:
await voice.disconnect()
voice = await voice_channel.connect()
voice.play(discord.FFmpegPCMAudio(source=source))
voice.is_playing()
CURRENT = podcast
await ctx.send(
f"`Now playing NiSERCast Episode {podcast} requested by {author}`")
else:
await ctx.channel.send(
content=
"_You are not connected to a voice channel. Please join a voice channel before invoking this command._"
)
@bot.command(name="pause", aliases=['//', 'pp'])
async def pause_(ctx):
"""
Pauses the currently playing episode
"""
vc = ctx.voice_client
if not vc or not vc.is_playing():
return await ctx.send('_I am currently not playing anything!_',
delete_after=60)
elif vc.is_paused():
return
vc.pause()
await ctx.send(f'**`{ctx.author}`**: Paused the episode!')
@bot.command(name="resume", aliases=['r'])
async def resume_(ctx):
"""
Resumes the currently paused episode
"""
vc = ctx.voice_client
if not vc or not vc.is_connected():
return await ctx.send('_I am currently not playing anything!_',
delete_after=60)
elif not vc.is_paused():
return
vc.resume()
await ctx.send(f'**`{ctx.author}`**: Resumed the episode!')
@bot.command(name="stop", aliases=['s', 'dc'])
async def stop_(ctx):
"""
Stops the currently playing episode and destroys the player
"""
vc = ctx.voice_client
if not vc or not vc.is_connected():
return await ctx.send("_I am currently not playing anything!_",
delete_after=60)
try:
await vc.disconnect()
except AttributeError:
pass
@bot.command(name="info", aliases=['i', 'np'])
async def info_(ctx):
"""
Displays information about the currently playing episode
"""
global CURRENT
if CURRENT is not None:
pod_num, pod_title, pod_published, pod_shortdesc = get_info(CURRENT)
# Preparing embed
file = discord.File("./castbot.png")
embed = discord.Embed(
title=pod_title,
description=
f"_Publication Timestamp: {pod_published}_\n\n{pod_shortdesc}",
colour=discord.Colour.gold(),
)
embed.add_field(name="\u200b", value="\u200b", inline=False)
embed.set_footer(
text=
"Custom Podcast Bot for NiSERCast Discord Server | Fork me on GitHub (https://github.com/sdgniser/castbot)",
icon_url="attachment://castbot.png")
await ctx.channel.send(content=None, file=file, embed=embed)
else:
await ctx.channel.send("_I am currently not playing anything!_",
delete_after=60)
@bot.command(name="help", help="Shows help message and list of commands")
async def help_(ctx):
"""
Shows help message and list of commands
"""
file = discord.File("./castbot.png")
embed = discord.Embed(
title="List of commands for CastBot",
description=
"CastBot announces new NiSERCast episodes and allows server members to play episodes within this server.\n\n**Bot Pefix**: `?`\n\n**List of Commands:**",
colour=discord.Colour.teal(),
)
embed.add_field(
name="`?announce`",
value=
"[**Admin only**] Puts a new episode announcement message in the current channel.",
inline=True)
embed.add_field(name="aliases", value="`?a`, `?<`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(
name="`?play <episode number>`",
value=
"Searches GitLab for the requested episode and plays it. Plays Ep. 1 by default. Join a voice channel before invoking this command.",
inline=True)
embed.add_field(name="aliases", value="`?p`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(name="`?pause`",
value="Pauses the currently playing episode.",
inline=True)
embed.add_field(name="aliases", value="`?pp`, `?//`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(name="`?resume`",
value="Resumes the currently paused episode.",
inline=True)
embed.add_field(name="aliases", value="`?r`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(
name="`?stop`",
value=
"Stops the currently playing episode and leaves the voice channel.",
inline=True)
embed.add_field(name="aliases", value="`?s`, `?dc`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(
name="`?info`",
value="Displays information about the currently playing episode.",
inline=True)
embed.add_field(name="aliases", value="`?i`, `?np`", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=True)
embed.add_field(name="\u200b", value="\u200b", inline=False)
embed.set_footer(
text=
"Custom Podcast Bot for NiSERCast Discord Server | Fork me on GitHub (https://github.com/sdgniser/castbot)",
icon_url="attachment://castbot.png")
await ctx.channel.send(content=None, file=file, embed=embed)
# Start-up and keep-alive
TOKEN = os.getenv("TOKEN")
keep_alive()
bot.run(TOKEN)