-
Notifications
You must be signed in to change notification settings - Fork 1
/
call_screener_rewrite.py
261 lines (208 loc) · 10.5 KB
/
call_screener_rewrite.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
import json
import logging
import traceback
import asyncio
import discord
from discord.ext import commands
description = 'A Discord call-screening bot for live radio shows.'
with open('config.json', 'r') as f:
config = json.load(f)
TOKEN = config['AUTH']['TOKEN']
CALL_IN_CHANNEL_NAME = config['CHANNELS']['CALL_IN']['name']
CALL_IN_CHANNEL_ID = config['CHANNELS']['CALL_IN']['id']
NONLIVE_CHANNEL_NAME = config['CHANNELS']['NONLIVE']['name']
NONLIVE_CHANNEL_ID = config['CHANNELS']['NONLIVE']['id']
SCREENING_CHANNEL_NAME = config['CHANNELS']['SCREENING']['name']
SCREENING_CHANNEL_ID = config['CHANNELS']['SCREENING']['id']
SHOW_CHANNEL_NAME = config['CHANNELS']['VOICE']['name']
SHOW_CHANNEL_ID = config['CHANNELS']['VOICE']['id']
HOST_IDS = config['HOSTS']
HOST_ROLE_ID = config['ROLES']['HOST_ROLE_ID']
CALLER_ROLE_ID = config['ROLES']['LIVE_CALLER_ID']
# Below cogs represents our folder our cogs are in.
# Following is the file name. So 'meme.py' in cogs, would be cogs.meme
# Think of it like a dot path import
initial_extensions = ['cogs.error']
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
)
bot = commands.Bot(command_prefix='!', description=description)
# Here we load our extensions(cogs) listed above in [initial_extensions].
if __name__ == '__main__':
for extension in initial_extensions:
try:
bot.load_extension(extension)
except Exception as e:
logging.error("Failed to load extension %s.", extension)
traceback.print_exc()
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Helper Check Decorators
# ------------------------------------------------------------------------------
def is_in_channel(id):
async def predicate(ctx):
return isinstance(ctx.channel, discord.TextChannel) and ctx.message.channel.id == id
return commands.check(predicate)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Other Methods - Non Commands
# ------------------------------------------------------------------------------
def name(member):
# A helper function to return the member's display name
nick = name = None
try:
nick = member.nick
except AttributeError:
pass
try:
name = member.name
except AttributeError:
pass
if nick:
return nick
if name:
return name
return None
async def is_live_show_happening(ctx):
show_channel = bot.get_channel(SHOW_CHANNEL_ID)
members = show_channel.members
member_ids = [member.id for member in members]
# Check if at least one host is in the live channel
hosts_in_channel = [host for host in HOST_IDS if host in member_ids]
if len(hosts_in_channel) > 0:
return True
else:
nonlive_channel = bot.get_channel(NONLIVE_CHANNEL_ID)
nonlive_msg = f'{ctx.author.mention} There is currently no live show. Please post your question in {nonlive_channel.mention} for the next show!'
await ctx.send(nonlive_msg)
return False
def is_anyone_mentioned(ctx):
try:
mentioned_user = ctx.message.mentions[0]
except:
mentioned_user = None
return mentioned_user
async def clean_and_add_livecallers(ctx, user=None):
# Clean Live Callers of any stale users
live_caller_role = discord.utils.find(lambda m: m.id == CALLER_ROLE_ID, ctx.guild.roles)
logging.info('Found Live Caller Role - %s', live_caller_role)
for member in live_caller_role.members:
if member != user:
msg_extra_live = f'FYI - I discovered that {member.name} was still in the Live Callers group while trying to add a new user. I have removed them now.'
await ctx.send(msg_extra_live)
await member.remove_roles(live_caller_role)
# Add Requested User to Live Caller Role
await user.add_roles(live_caller_role)
async def clean_livecallers(ctx):
# Clean Live Callers of any stale users
live_caller_role = discord.utils.find(lambda m: m.id == CALLER_ROLE_ID, ctx.guild.roles)
logging.info('Found Live Caller Role - %s', live_caller_role)
for member in live_caller_role.members:
await member.remove_roles(live_caller_role)
async def gather_caller_info(author):
# Implement wait_for check (is author & DM)
def check(m):
return m.author == author and isinstance(m.channel, discord.DMChannel)
await author.send("Thanks for wanting to call in. Before we get you on the line, let's get a few details.")
# Ask Question 1
await author.send("What should we call you?")
caller_name = await bot.wait_for('message', timeout=30, check=check)
caller_name = caller_name.content
# Ask Question 2
await author.send(f'Hey {caller_name} - where are you from?')
caller_location = await bot.wait_for('message', timeout=30, check=check)
caller_location = caller_location.content
# Ask Question 3
await author.send(f'Thanks for that {caller_name} - what would you like to discuss?')
caller_topic = await bot.wait_for('message', timeout=30, check=check)
caller_topic = caller_topic.content
# Send confirmation message
caller_details = f'{caller_name} from {caller_location} wants to talk about - {caller_topic}'
await author.send(f'We will send the following message to the live show screening channel.\n'
f'`{caller_details}`\n\nIf this is correct, reply with the word YES.')
caller_confirm = await bot.wait_for('message', timeout=30, check=check)
if 'YES' in caller_confirm.content.upper():
e = discord.Embed(title='NEW CALLER ALERT!', description=caller_details)
# e.set_thumbnail(url=author.avatar_url)
e.add_field(name='\a', value='\a', inline=False) # Blank line (empty field)
e.add_field(name='To add the caller:', value=f"!{config['COMMANDS']['answer']} {author.mention}", inline=False)
e.add_field(name='To remove the caller:', value=f"!{config['COMMANDS']['answer']}", inline=False)
screening_channel = bot.get_channel(SCREENING_CHANNEL_ID)
await screening_channel.send(embed=e)
await author.send('Awesome - thanks! Your message has been sent '
'and you will be notified when you are dialed into the live show!')
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Bot Commands
# ------------------------------------------------------------------------------
@bot.event
async def on_ready():
logging.info("Logged in as: %s", bot.user.name)
logging.info('Version: %s', discord.__version__)
logging.info('-' * 10)
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="the phones."))
@bot.event
async def on_voice_state_update(member, before, after):
live_caller_role = discord.utils.find(lambda m: m.id == CALLER_ROLE_ID, member.guild.roles)
screening_channel = discord.utils.find(lambda m: m.id == SCREENING_CHANNEL_ID, member.guild.channels)
show_channel = discord.utils.find(lambda m: m.id == SHOW_CHANNEL_ID, member.guild.channels)
is_live_caller = bool(member in live_caller_role.members)
# If a Live Caller drops from a voice channel
if after.channel != show_channel and is_live_caller:
msg_user_voice = f"Live Caller '{name(member)}' has dropped from the Live Show voice channel!"
await screening_channel.send(msg_user_voice)
return
# If a Live Caller joines the voice channel
if after.channel == show_channel and is_live_caller:
msg_user_voice = f"Live Caller '{name(member)}' has joined the Live Show voice channel!"
await screening_channel.send(msg_user_voice)
@bot.command(name=config['COMMANDS']['call'])
@commands.cooldown(1, 30, commands.BucketType.user)
@is_in_channel(CALL_IN_CHANNEL_ID)
async def call(ctx):
logging.info("Command '%s' detected in call-in channel (%s).", ctx.command.name, CALL_IN_CHANNEL_NAME)
# Check if there is a Live Show in Progress
if not await is_live_show_happening(ctx):
return
# If there is a live show in progress, get caller information
try:
await gather_caller_info(ctx.message.author)
except asyncio.TimeoutError:
await ctx.message.author.send(f"We haven't heard from you in a while."
f"If you'd like to call back in, please issue the `!call` command again!")
return
@bot.command(name=config['COMMANDS']['answer'])
@commands.has_role(HOST_ROLE_ID)
@is_in_channel(SCREENING_CHANNEL_ID)
async def answer(ctx):
logging.info("Command '%s' detected in call screening channel (%s).", ctx.command.name, SCREENING_CHANNEL_NAME)
# Check if this command mentions anyone (invalid without a Member object)
user = is_anyone_mentioned(ctx)
if user is None:
await ctx.send(f'{ctx.author.mention} you need to mention a user for this command to work properly.')
return
# Clean Live Callers & add requested user
await clean_and_add_livecallers(ctx, user)
# Check if user is listening to the live show
show_channel = bot.get_channel(SHOW_CHANNEL_ID)
live_listeners = show_channel.members
if user in live_listeners:
add_msg = f'{name(user)} has been added to the Live Caller role and can speak in the voice channel.'
msg_user_notify = f'You are now connected to the live show!'
else:
add_msg = (f'{name(user)} has been added to the Live Caller role, but is not yet in the voice channel. '
f'I will let you know when they join.')
msg_user_notify = (f'You are now connected to the live show!'
f'Please be sure you are connected to the {show_channel.mention} channel to talk.')
# Send the Screening notification & User a DM for the live call-in
await ctx.send(add_msg)
await user.send(msg_user_notify)
@bot.command(name=config['COMMANDS']['hangup'])
@commands.has_role(HOST_ROLE_ID)
@is_in_channel(SCREENING_CHANNEL_ID)
async def hangup(ctx):
logging.info("Command '%s' detected in call screening channel (%s).", ctx.command.name, SCREENING_CHANNEL_NAME)
# Remove all members from the Live Callers role
await clean_livecallers(ctx)
bot.run(TOKEN, bot=True, reconnect=True)