pub struct Context {
pub http: Arc<Http>,
pub cache: Arc<Cache>,
pub gateway_tx: Arc<Sender<String>>,
pub voice_states: Arc<Mutex<HashMap<String, VoiceState>>>,
/* private fields */
}Expand description
Shared state passed into every event handler call. This is how you interact with the API from inside your event handlers.
ctx.http.send_message("channel_id", "Hello!").await.unwrap();Fields§
§http: Arc<Http>HTTP client for REST API calls.
cache: Arc<Cache>In-memory cache populated automatically from gateway events.
gateway_tx: Arc<Sender<String>>Raw gateway sender. You probably won’t need this directly – voice join/leave use it internally.
voice_states: Arc<Mutex<HashMap<String, VoiceState>>>Per-guild voice state, populated from VOICE_STATE_UPDATE and VOICE_SERVER_UPDATE.
Used internally by join_voice / leave_voice.
Implementations§
Source§impl Context
impl Context
Sourcepub async fn join_voice(
&self,
guild_id: &str,
channel_id: &str,
) -> Result<FluxerVoiceConnection, ClientError>
pub async fn join_voice( &self, guild_id: &str, channel_id: &str, ) -> Result<FluxerVoiceConnection, ClientError>
Joins a voice channel. Sends an opcode 4 to the gateway and waits up to 10 seconds for the server to send back connection details.
Examples found in repository?
examples/voice.rs (line 55)
27 async fn on_message(&self, ctx: Context, msg: Message) {
28 if msg.author.bot.unwrap_or(false) {
29 return;
30 }
31
32 let content = match msg.content.as_deref() {
33 Some(c) => c,
34 None => return,
35 };
36
37 let channel_id = msg.channel_id.as_deref().unwrap_or_default();
38 let guild_id = match msg.guild_id.as_deref() {
39 Some(id) => id,
40 None => return,
41 };
42
43 let (cmd, args) = match parse_command(content) {
44 Some(v) => v,
45 None => return,
46 };
47
48 match cmd {
49 "join" => {
50 if args.is_empty() {
51 let _ = ctx.http.send_message(channel_id, "`!join <voice_channel_id>`").await;
52 return;
53 }
54
55 match ctx.join_voice(guild_id, args).await {
56 Ok(conn) => {
57 *self.voice.lock().await = Some(conn);
58 let _ = ctx.http.send_message(channel_id, "Joined.").await;
59 }
60 Err(e) => {
61 let _ = ctx.http.send_message(channel_id, &format!("Failed: {}", e)).await;
62 }
63 }
64 }
65
66 "leave" => {
67 if let Some(handle) = self.playback.lock().await.take() {
68 handle.abort();
69 }
70 *self.voice.lock().await = None;
71 let _ = ctx.leave_voice(guild_id).await;
72 let _ = ctx.http.send_message(channel_id, "Left.").await;
73 }
74
75 "play" => {
76 let conn = self.voice.lock().await;
77 let conn = match conn.as_ref() {
78 Some(c) => c,
79 None => {
80 let _ = ctx.http.send_message(channel_id, "Not in a voice channel.").await;
81 return;
82 }
83 };
84
85 if let Some(handle) = self.playback.lock().await.take() {
86 handle.abort();
87 }
88
89 match conn.play_music(AUDIO_FILE, ctx.http.clone(), channel_id.to_string()).await {
90 Ok(handle) => {
91 *self.playback.lock().await = Some(handle);
92 let _ = ctx.http.send_message(channel_id, &format!("Playing `{}`.", AUDIO_FILE)).await;
93 }
94 Err(e) => {
95 let _ = ctx.http.send_message(channel_id, &format!("Failed: {}", e)).await;
96 }
97 }
98 }
99
100 "stop" => {
101 if let Some(handle) = self.playback.lock().await.take() {
102 handle.abort();
103 let _ = ctx.http.send_message(channel_id, "Stopped.").await;
104 } else {
105 let _ = ctx.http.send_message(channel_id, "Nothing is playing.").await;
106 }
107 }
108
109 _ => {}
110 }
111 }Sourcepub async fn leave_voice(&self, guild_id: &str) -> Result<(), ClientError>
pub async fn leave_voice(&self, guild_id: &str) -> Result<(), ClientError>
Leaves a voice channel. Closes the LiveKit room and tells the gateway.
Examples found in repository?
examples/voice.rs (line 71)
27 async fn on_message(&self, ctx: Context, msg: Message) {
28 if msg.author.bot.unwrap_or(false) {
29 return;
30 }
31
32 let content = match msg.content.as_deref() {
33 Some(c) => c,
34 None => return,
35 };
36
37 let channel_id = msg.channel_id.as_deref().unwrap_or_default();
38 let guild_id = match msg.guild_id.as_deref() {
39 Some(id) => id,
40 None => return,
41 };
42
43 let (cmd, args) = match parse_command(content) {
44 Some(v) => v,
45 None => return,
46 };
47
48 match cmd {
49 "join" => {
50 if args.is_empty() {
51 let _ = ctx.http.send_message(channel_id, "`!join <voice_channel_id>`").await;
52 return;
53 }
54
55 match ctx.join_voice(guild_id, args).await {
56 Ok(conn) => {
57 *self.voice.lock().await = Some(conn);
58 let _ = ctx.http.send_message(channel_id, "Joined.").await;
59 }
60 Err(e) => {
61 let _ = ctx.http.send_message(channel_id, &format!("Failed: {}", e)).await;
62 }
63 }
64 }
65
66 "leave" => {
67 if let Some(handle) = self.playback.lock().await.take() {
68 handle.abort();
69 }
70 *self.voice.lock().await = None;
71 let _ = ctx.leave_voice(guild_id).await;
72 let _ = ctx.http.send_message(channel_id, "Left.").await;
73 }
74
75 "play" => {
76 let conn = self.voice.lock().await;
77 let conn = match conn.as_ref() {
78 Some(c) => c,
79 None => {
80 let _ = ctx.http.send_message(channel_id, "Not in a voice channel.").await;
81 return;
82 }
83 };
84
85 if let Some(handle) = self.playback.lock().await.take() {
86 handle.abort();
87 }
88
89 match conn.play_music(AUDIO_FILE, ctx.http.clone(), channel_id.to_string()).await {
90 Ok(handle) => {
91 *self.playback.lock().await = Some(handle);
92 let _ = ctx.http.send_message(channel_id, &format!("Playing `{}`.", AUDIO_FILE)).await;
93 }
94 Err(e) => {
95 let _ = ctx.http.send_message(channel_id, &format!("Failed: {}", e)).await;
96 }
97 }
98 }
99
100 "stop" => {
101 if let Some(handle) = self.playback.lock().await.take() {
102 handle.abort();
103 let _ = ctx.http.send_message(channel_id, "Stopped.").await;
104 } else {
105 let _ = ctx.http.send_message(channel_id, "Nothing is playing.").await;
106 }
107 }
108
109 _ => {}
110 }
111 }Trait Implementations§
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more