Skip to content

Instantly share code, notes, and snippets.

@jeroldhaas
Created April 20, 2016 03:05
Show Gist options
  • Save jeroldhaas/1ae35a3d0c84e001da9e36fd59db386d to your computer and use it in GitHub Desktop.
Save jeroldhaas/1ae35a3d0c84e001da9e36fd59db386d to your computer and use it in GitHub Desktop.
Use DiscordExample as "Inspriration" to F# Example
#load "Scripts/load-references-debug.fsx"
open System
open System.Collections.Generic
open FSharp.Core
open FSharp.Collections
open System.Linq
open FSharp.Linq // probably won't use, but...
open System.Text
open System.Threading.Tasks
open Discord
open Discord.Commands
open Discord.Modules
let appName = "Eff Sharp Bot"
// TODO: further exploration: look into permission levels
type PermissionLevel =
| User = 0
| ChannelModerator = 1
| ChannelAdmin = 2
| ServerModerator = 3
| ServerAdmin = 4
| ServerOwner = 5
| BotOwner = 6
let getPermissions (u: User) (c: Channel) : PermissionLevel =
let isBotCommander =
u.Roles
|> Seq.toList
|> List.map (fun x -> x.Name.ToLower())
|> List.contains "bot commander"
let serverPerms = u.ServerPermissions
let chanPerms = u.GetPermissions(c)
match (u, c) with
| (u, _) when u.Id = (uint64 5920) -> PermissionLevel.BotOwner
| (_, c) when not c.IsPrivate ->
if (serverPerms.ManageRoles ||
isBotCommander) then PermissionLevel.ServerAdmin
elif (serverPerms.ManageServer &&
serverPerms.KickMembers &&
serverPerms.BanMembers) then PermissionLevel.ServerModerator
elif chanPerms.ManagePermissions then PermissionLevel.ChannelAdmin
elif chanPerms.ManageMessages then PermissionLevel.ChannelModerator
else PermissionLevel.User
| (_, _) -> PermissionLevel.User
type DiscordClient with
member x.UsingCommands(f: Action<CommandServiceConfigBuilder>) =
Discord.Commands.CommandExtensions.UsingCommands(x, f)
member x.UsingPermissionLevels(f: Func<User, Channel, int>) =
Discord.Commands.Permissions.Levels.PermissionLevelExtensions.UsingPermissionLevels(x, f)
let client =
let c = new DiscordClient(fun x ->
x.AppName <- appName
x.MessageCacheSize <- 10
x.EnablePreUpdateEvents <- true
)
c.UsingCommands(fun x ->
x.AllowMentionPrefix <- true
x.HelpMode <- HelpMode.Public)
.UsingPermissionLevels(fun u c ->
getPermissions u c |> int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment