Signed-off-by: Ken Swenson <flat@esoteric.moe>
---
src/main.rs | 87 +++++++++++++++++++++++++++--------------------------
1 file changed, 45 insertions(+), 42 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 4801e73..287fc9a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -163,7 +163,7 @@ async fn async_main() -> GarfieldResult<()> {
let mut events = cluster.events().await;
- let ctx = Context::new(http, rqc, theme_client, parser, cluster, subs, cache.clone());
+ let ctx = Context(Arc::new(ContextRef::new(http, rqc, theme_client, parser, cluster, subs, cache.clone())));
while let Some((_, event)) = events.next().await {
trace!("New Event: {:?}", event.kind());
@@ -194,13 +194,13 @@ async fn handle_event(event: Event, ctx: Context<'_>) -> GarfieldResult<()> {
}
async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
- if let Some(command) = ctx.parser.parse(&msg.content) {
+ if let Some(command) = ctx.0.parser.parse(&msg.content) {
match command {
Command { name: "today", .. } => {
info!("Sending TODAY to: {:?}", msg);
ComicEmbed::new()
.today()
- .send(&ctx.http, msg.channel_id)
+ .send(&ctx.0.http, msg.channel_id)
.await?;
Ok(())
}
@@ -213,7 +213,7 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let date = arguments.as_str();
ComicEmbed::new()
.date_str(date)
- .send(&ctx.http, msg.channel_id)
+ .send(&ctx.0.http, msg.channel_id)
.await?;
Ok(())
}
@@ -224,8 +224,8 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
} => {
info!("Sending THEME to: {:?}", msg);
let theme = arguments.as_str();
- let comic = ctx.tc.get_theme(theme).await?;
- comic.send(&ctx.http, msg.channel_id).await?;
+ let comic = ctx.0.tc.get_theme(theme).await?;
+ comic.send(&ctx.0.http, msg.channel_id).await?;
Ok(())
}
Command {
@@ -234,7 +234,7 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
info!("Sending TOMORROW to: {:?}", msg);
ComicEmbed::new()
.tomorrow()
- .send(&ctx.http, msg.channel_id)
+ .send(&ctx.0.http, msg.channel_id)
.await?;
Ok(())
}
@@ -242,7 +242,7 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
info!("Sending RANDOM to: {:?}", msg);
ComicEmbed::new()
.random()
- .send(&ctx.http, msg.channel_id)
+ .send(&ctx.0.http, msg.channel_id)
.await?;
Ok(())
}
@@ -252,13 +252,13 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
info!("Sending YESTERDAY to: {:?}", msg);
ComicEmbed::new()
.yesterday()
- .send(&ctx.http, msg.channel_id)
+ .send(&ctx.0.http, msg.channel_id)
.await?;
Ok(())
}
Command { name: "help", .. } => {
info!("Sending HELP to: {:?}", msg);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(HELP)?
.await?;
@@ -266,7 +266,7 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
}
Command { name: "about", .. } => {
info!("Sending ABOUT to: {:?}", msg);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(ABOUT)?
.await?;
@@ -279,13 +279,13 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
} => {
if msg.author.id == OWNER {
println!("GETTING SHARD");
- if let Some(shard) = ctx.cls.shard(0).await {
+ if let Some(shard) = ctx.0.cls.shard(0).await {
use twilight::model::gateway::payload::update_status::UpdateStatus;
use twilight::model::gateway::presence::Status::Online;
let name = arguments.as_str();
let com = UpdateStatus::new(false, activity(name), None, Online);
shard.command(&com).await?;
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("Set status to:\n{}", name))?
.await?;
@@ -304,8 +304,8 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
Some(i) => i,
None => GuildId(msg.author.id.0),
};
- syndicate::syndicate(&ctx.syndicate, id, msg.channel_id)?;
- ctx.http
+ syndicate::syndicate(&ctx.0.syndicate, id, msg.channel_id)?;
+ ctx.0.http
.create_message(msg.channel_id)
.content(
"Syndicates to this channel. \
@@ -326,22 +326,22 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
Some(i) => i,
None => GuildId(msg.author.id.0),
};
- syndicate::stop_syndication(&ctx.syndicate, id)?;
- ctx.http
+ syndicate::stop_syndication(&ctx.0.syndicate, id)?;
+ ctx.0.http
.create_message(msg.channel_id)
.content("Does no longer syndicate to this channel.")?
.await?;
Ok(())
}
Command { name: "stats", .. } => {
- let users = ctx.cache.closure(|c| c.users.lock().len());
- let members = ctx.cache.closure(|c| c.members.lock().len());
- let guilds = ctx.cache.closure(|c| c.guilds.lock().len());
- let emojis = ctx.cache.closure(|c| c.emojis.lock().len());
- let channels = ctx.cache.closure(|c| c.channels.lock().len());
- let presences = ctx.cache.closure(|c| c.presences.lock().len());
-
- ctx.http
+ let users = ctx.0.cache.closure(|c| c.users.lock().len());
+ let members = ctx.0.cache.closure(|c| c.members.lock().len());
+ let guilds = ctx.0.cache.closure(|c| c.guilds.lock().len());
+ let emojis = ctx.0.cache.closure(|c| c.emojis.lock().len());
+ let channels = ctx.0.cache.closure(|c| c.channels.lock().len());
+ let presences = ctx.0.cache.closure(|c| c.presences.lock().len());
+
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!(
"Users: {}\n\
@@ -367,9 +367,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = UserId(arguments.as_str().parse::<u64>()?);
- let user = ctx.cache.user(id);
+ let user = ctx.0.cache.user(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", user))?
.await?;
@@ -392,9 +392,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let gid = GuildId(args[0].parse()?);
let uid = UserId(args[1].parse()?);
- let member = ctx.cache.member(gid, uid);
+ let member = ctx.0.cache.member(gid, uid);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", member))?
.await?;
@@ -412,9 +412,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = GuildId(arguments.as_str().parse::<u64>()?);
- let guild = ctx.cache.guild(id);
+ let guild = ctx.0.cache.guild(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", guild))?
.await?;
@@ -432,9 +432,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = RoleId(arguments.as_str().parse::<u64>()?);
- let role = ctx.cache.role(id);
+ let role = ctx.0.cache.role(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", role))?
.await?;
@@ -452,9 +452,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = EmojiId(arguments.as_str().parse::<u64>()?);
- let emoji = ctx.cache.emoji(id);
+ let emoji = ctx.0.cache.emoji(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", emoji))?
.await?;
@@ -472,9 +472,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = ChannelId(arguments.as_str().parse::<u64>()?);
- let channel = ctx.cache.channel(id);
+ let channel = ctx.0.cache.channel(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", channel))?
.await?;
@@ -492,9 +492,9 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
let id = UserId(arguments.as_str().parse::<u64>()?);
- let presence = ctx.cache.presence(id);
+ let presence = ctx.0.cache.presence(id);
- ctx.http
+ ctx.0.http
.create_message(msg.channel_id)
.content(format!("```\n{:?}\n```", presence))?
.await?;
@@ -510,7 +510,7 @@ async fn handle_command(msg: Message, ctx: Context<'_>) -> GarfieldResult<()> {
}
#[derive(Clone)]
-struct Context<'a> {
+struct ContextRef<'a> {
pub http: HttpClient,
pub rqc: ReqwestClient,
pub tc: Arc<ThemeClient>,
@@ -520,7 +520,7 @@ struct Context<'a> {
pub cache: sled_cache::InMemoryCache,
}
-impl<'a> Context<'a> {
+impl<'a> ContextRef<'a> {
fn new(
http: HttpClient,
rqc: ReqwestClient,
@@ -531,7 +531,7 @@ impl<'a> Context<'a> {
cache: sled_cache::InMemoryCache,
) -> Self {
let tc = Arc::new(tc);
- Context {
+ ContextRef {
http: http,
rqc: rqc,
tc,
@@ -543,6 +543,9 @@ impl<'a> Context<'a> {
}
}
+#[derive(Clone)]
+pub struct Context<'a>(Arc<ContextRef<'a>>);
+
fn activity(name: &str) -> Activity {
Activity {
application_id: None,
--
2.27.0