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
|
use std::{
env,
str::FromStr,
};
use serenity::{
client::Context,
model::{
id::{
ChannelId,
MessageId,
},
permissions::Permissions,
}
};
use url::Url;
use crate::{
audio::PlayQueue,
Result,
};
pub trait CtxExt {
fn currently_playing(&self) -> bool;
fn users_listening(&self) -> Result<bool>;
fn send<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()>;
fn send_result<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<MessageId>;
}
impl CtxExt for Context {
fn currently_playing(&self) -> bool {
let queue_lock = self.data.read().get::<PlayQueue>().cloned().unwrap();
let play_queue = queue_lock.read().unwrap();
play_queue.playing.is_some()
}
fn users_listening(&self) -> Result<bool> {
let channel_id = ChannelId(must_env_lookup::<u64>("VOICE_CHANNEL"));
let channel = channel_id.to_channel(self)?;
let res = channel.guild()
.and_then(|ch| ch.read().guild(self))
.map(|g| (&g.read().voice_states)
.into_iter()
.any(|(_, state)| state.channel_id == Some(channel_id)))
.unwrap_or(false);
Ok(res)
}
#[inline]
fn send<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<()> {
self.send_result(channel, text, tts).map(|_| ())
}
#[inline]
fn send_result<A: AsRef<str>>(&self, channel: ChannelId, text: A, tts: bool) -> Result<MessageId> {
let result = channel.send_message(self, |m| m.content(text.as_ref()).tts(tts))?;
Ok(result.id)
}
}
lazy_static! {
static ref REQUIRED_PERMS: Permissions = Permissions::EMBED_LINKS |
Permissions::READ_MESSAGES |
Permissions::ADD_REACTIONS |
Permissions::SEND_MESSAGES |
Permissions::SEND_TTS_MESSAGES |
Permissions::MENTION_EVERYONE |
Permissions::USE_EXTERNAL_EMOJIS |
Permissions::CONNECT |
Permissions::SPEAK |
Permissions::CHANGE_NICKNAME |
Permissions::USE_VAD |
Permissions::ATTACH_FILES;
}
lazy_static! {
pub static ref OAUTH_URL: Url = Url::parse(
&format!(
"https://discordapp.com/api/oauth2/authorize?scope=bot&permissions={}&client_id={}",
REQUIRED_PERMS.bits(), dotenv!("THULANI_CLIENT_ID"),
)
).unwrap();
}
pub fn must_env_lookup<T: FromStr>(s: &str) -> T {
env::var(s).expect(&format!("missing env var {}", s))
.parse::<T>().unwrap_or_else(|_| panic!(format!("bad format for {}", s)))
}
|