| 1147 | } |
| 1148 | |
| 1149 | pub async fn try_guild_add_script_plugin( |
| 1150 | &self, |
| 1151 | guild_id: Id<GuildMarker>, |
| 1152 | plugin_id: u64, |
| 1153 | auto_update: bool, |
| 1154 | ) -> ConfigStoreResult<Script> { |
| 1155 | let mut tx = self.pool.begin().await?; |
| 1156 | |
| 1157 | let scripts = Self::get_guild_scripts(&mut tx, guild_id).await?; |
| 1158 | if scripts.iter().any(|v| v.plugin_id == Some(plugin_id)) { |
| 1159 | return Err(ConfigStoreError::GuildAlreadyHasPlugin); |
| 1160 | } |
| 1161 | |
| 1162 | let plugin = Self::inner_get_plugin(&mut tx, plugin_id).await?; |
| 1163 | |
| 1164 | let source = match plugin.data { |
| 1165 | PluginData::ScriptPlugin(d) => d.published_version.unwrap_or_default(), |
| 1166 | }; |
| 1167 | |
| 1168 | let created = Self::inner_create_script( |
| 1169 | &mut tx, |
| 1170 | guild_id, |
| 1171 | CreateScript { |
| 1172 | name: plugin.name.clone(), |
| 1173 | original_source: source, |
| 1174 | enabled: true, |
| 1175 | plugin_auto_update: Some(auto_update), |
| 1176 | plugin_id: Some(plugin_id), |
| 1177 | plugin_version_number: Some(plugin.current_version), |
| 1178 | }, |
| 1179 | ) |
| 1180 | .await?; |
| 1181 | |
| 1182 | tx.commit().await?; |
| 1183 | |
| 1184 | Ok(created) |
| 1185 | } |
| 1186 | |
| 1187 | pub async fn get_plugin_image( |
| 1188 | &self, |