本文整理汇总了C#中Discord.DiscordClient.AddService方法的典型用法代码示例。如果您正苦于以下问题:C# DiscordClient.AddService方法的具体用法?C# DiscordClient.AddService怎么用?C# DiscordClient.AddService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Discord.DiscordClient
的用法示例。
在下文中一共展示了DiscordClient.AddService方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
Playlist = new List<string>();
var inoriClient = new DiscordClient(x =>
{
x.AppName = "MilliaBot";
x.MessageCacheSize = 0;
x.UsePermissionsCache = true;
x.EnablePreUpdateEvents = true;
x.LogLevel = LogSeverity.Debug;
})
.UsingCommands(x =>
{
x.AllowMentionPrefix = true;
x.HelpMode = HelpMode.Public;
})
.UsingModules();
inoriClient.AddService<AudioService>(new AudioService(new AudioServiceConfigBuilder()
{
Channels = 2,
EnableEncryption = false,
EnableMultiserver = false,
Bitrate = 96,
}));
//Display all log messages in the console
inoriClient.Log.Message += (s, e) => Console.WriteLine($"[{e.Severity}] {e.Source}: {e.Message}");
HandleMessage(inoriClient);
//Convert our sync method to an async one and block the Main function until the bot disconnects
Connect(inoriClient);
}
示例2: Main
//.........这里部分代码省略.........
//create new discord client and log
Client = new DiscordClient(new DiscordConfigBuilder()
{
MessageCacheSize = 10,
ConnectionTimeout = int.MaxValue,
LogLevel = LogSeverity.Warning,
LogHandler = (s, e) =>
Console.WriteLine($"Severity: {e.Severity}" +
$"ExceptionMessage: {e.Exception?.Message ?? "-"}" +
$"Message: {e.Message}"),
});
//create a command service
var commandService = new CommandService(new CommandServiceConfigBuilder
{
AllowMentionPrefix = false,
CustomPrefixHandler = m => 0,
HelpMode = HelpMode.Disabled,
ErrorHandler = async (s, e) =>
{
if (e.ErrorType != CommandErrorType.BadPermissions)
return;
if (string.IsNullOrWhiteSpace(e.Exception?.Message))
return;
try
{
await e.Channel.SendMessage(e.Exception.Message).ConfigureAwait(false);
}
catch { }
}
});
//add command service
Client.AddService<CommandService>(commandService);
//create module service
var modules = Client.AddService<ModuleService>(new ModuleService());
//add audio service
Client.AddService<AudioService>(new AudioService(new AudioServiceConfigBuilder()
{
Channels = 2,
EnableEncryption = false,
Bitrate = 128,
}));
//install modules
modules.Add(new HelpModule(), "Help", ModuleFilter.None);
modules.Add(new AdministrationModule(), "Administration", ModuleFilter.None);
modules.Add(new UtilityModule(), "Utility", ModuleFilter.None);
modules.Add(new PermissionModule(), "Permissions", ModuleFilter.None);
modules.Add(new Conversations(), "Conversations", ModuleFilter.None);
modules.Add(new GamblingModule(), "Gambling", ModuleFilter.None);
modules.Add(new GamesModule(), "Games", ModuleFilter.None);
#if !NADEKO_RELEASE
modules.Add(new MusicModule(), "Music", ModuleFilter.None);
#endif
modules.Add(new SearchesModule(), "Searches", ModuleFilter.None);
modules.Add(new NSFWModule(), "NSFW", ModuleFilter.None);
modules.Add(new ClashOfClansModule(), "ClashOfClans", ModuleFilter.None);
modules.Add(new PokemonModule(), "Pokegame", ModuleFilter.None);
modules.Add(new TranslatorModule(), "Translator", ModuleFilter.None);
modules.Add(new CustomReactionsModule(), "Customreactions", ModuleFilter.None);
if (!string.IsNullOrWhiteSpace(Creds.TrelloAppKey))
modules.Add(new TrelloModule(), "Trello", ModuleFilter.None);
示例3: Start
private static void Start()
{
SettingsManager.Load();
_client = new DiscordClient();
_client.LogMessage += (s, e) =>
{
Console.WriteLine($"[{e.Severity}] {e.Source} => {e.Message}");
};
//Set up permissions
_client.AddService(new BlacklistService());
_client.AddService(new WhitelistService());
_client.AddService(new PermissionLevelService((u, c) =>
{
if (u.Id == long.Parse(SettingsManager.OwnerID))
return (int)PermissionLevel.BotOwner;
if (!u.IsPrivate)
{
if (u == c.Server.Owner)
return (int)PermissionLevel.ServerOwner;
var serverPerms = u.GetServerPermissions();
if (serverPerms.ManageRoles)
return (int)PermissionLevel.ServerAdmin;
if (serverPerms.ManageMessages && serverPerms.KickMembers && serverPerms.BanMembers)
return (int)PermissionLevel.ServerMod;
var channelPerms = u.GetPermissions(c);
if (channelPerms.ManagePermissions)
return (int)PermissionLevel.ChannelAdmin;
if (channelPerms.ManageMessages)
return (int)PermissionLevel.ChannelMod;
}
return (int)PermissionLevel.User;
}));
//Set up commands
var commands = _client.AddService(new CommandService(new CommandServiceConfig
{
CommandChar = '!',
HelpMode = HelpMode.Private
}));
commands.RanCommand += (s, e) => Console.WriteLine($"[Command] {(e.Server == null ? "[Private]" : e.Server.ToString()) + "/" + e.Channel} => {e.Message}");
commands.CommandError += (s, e) =>
{
string msg = e.Exception?.GetBaseException().Message;
if (msg == null)
{
{
switch (e.ErrorType)
{
case CommandErrorType.Exception:
msg = "Unknown error.";
break;
case CommandErrorType.BadPermissions:
msg = "You do not have permission to run this command.";
break;
case CommandErrorType.BadArgCount:
msg = "You provided the incorrect number of arguments for this command.";
break;
case CommandErrorType.InvalidInput:
msg = "Unable to parse your command, please check your input.";
break;
case CommandErrorType.UnknownCommand:
msg = "Unknown command.";
break;
}
}
}
if (msg != null)
{
_client.SendMessage(e.Channel, $"Failed to complete command: {msg}");
Console.WriteLine($"[Error] Failed to complete command: {e.Command?.Text} for {e.User?.Name}");
Console.WriteLine($"Command failure: {msg}");
}
};
//Set up modules
var modules = _client.AddService(new ModuleService());
//Boot up
_client.Run(async () =>
{
while (true)
{
try
{
await _client.Connect(SettingsManager.Email, SettingsManager.Password);
if (!_client.AllServers.Any())
await _client.AcceptInvite(_client.GetInvite("0nwaapOqh2LPqDL9").Result);
modules.Install(new Modules.SimpleCommands(), "Simple Commands", FilterType.Unrestricted);
modules.Install(new Modules.Chance(), "Dice Rolling", FilterType.Unrestricted);
break;
}
catch (Exception ex)
{
Console.WriteLine("Login failed" + ex.ToString());
//.........这里部分代码省略.........
示例4: Start
private void Start(string[] args)
{
#if !DNXCORE50
Console.Title = $"{AppName} (Discord.Net v{DiscordConfig.LibVersion})";
#endif
GlobalSettings.Load();
_client = new DiscordClient(x =>
{
x.AppName = AppName;
x.AppUrl = AppUrl;
x.MessageCacheSize = 0;
x.UsePermissionsCache = true;
x.EnablePreUpdateEvents = true;
x.LogLevel = LogSeverity.Debug;
x.LogHandler = OnLogMessage;
})
.UsingCommands(x =>
{
x.AllowMentionPrefix = true;
x.HelpMode = HelpMode.Public;
x.ExecuteHandler = OnCommandExecuted;
x.ErrorHandler = OnCommandError;
})
.UsingModules()
.UsingAudio(x =>
{
x.Mode = AudioMode.Outgoing;
x.EnableMultiserver = true;
x.EnableEncryption = true;
x.Bitrate = AudioServiceConfig.MaxBitrate;
x.BufferLength = 10000;
})
.UsingPermissionLevels(PermissionResolver);
_client.AddService<SettingsService>();
_client.AddService<HttpService>();
_client.AddModule<AdminModule>("Admin", ModuleFilter.ServerWhitelist);
_client.AddModule<ColorsModule>("Colors", ModuleFilter.ServerWhitelist);
_client.AddModule<FeedModule>("Feeds", ModuleFilter.ServerWhitelist);
_client.AddModule<GithubModule>("Repos", ModuleFilter.ServerWhitelist);
_client.AddModule<ModulesModule>("Modules", ModuleFilter.None);
_client.AddModule<PublicModule>("Public", ModuleFilter.None);
_client.AddModule<TwitchModule>("Twitch", ModuleFilter.ServerWhitelist);
_client.AddModule<StatusModule>("Status", ModuleFilter.ServerWhitelist);
//_client.AddModule(new ExecuteModule(env, exporter), "Execute", ModuleFilter.ServerWhitelist);
#if PRIVATE
PrivateModules.Install(_client);
#endif
//Convert this method to an async function and connect to the server
//DiscordClient will automatically reconnect once we've established a connection, until then we loop on our end
//Note: ExecuteAndWait is only needed for Console projects as Main can't be declared as async. UI/Web applications should *not* use this function.
_client.ExecuteAndWait(async () =>
{
while (true)
{
try
{
await _client.Connect(GlobalSettings.Discord.Email, GlobalSettings.Discord.Password);
_client.SetGame("Discord.Net");
//await _client.ClientAPI.Send(new Discord.API.Client.Rest.HealthRequest());
break;
}
catch (Exception ex)
{
_client.Log.Error($"Login Failed", ex);
await Task.Delay(_client.Config.FailedReconnectDelay);
}
}
});
}
示例5: Main
static void Main(string[] args)
{
// Load up the DB, or create it if it doesn't exist
SQL.LoadDB();
// Load up the config file
LoadConfig();
Console.Title = $"Nekobot v{version}";
// Load the stream channels
Music.LoadStreams();
// Initialize rest client
RCInit();
client = new DiscordClient(new DiscordClientConfig
{
AckMessages = true,
LogLevel = LogMessageSeverity.Verbose,
TrackActivity = true,
UseMessageQueue = false,
UseLargeThreshold = true,
EnableVoiceMultiserver = true,
VoiceMode = DiscordVoiceMode.Outgoing,
});
// Set up the events and enforce use of the command prefix
commands.CommandError += CommandError;
client.Connected += Connected;
client.Disconnected += Disconnected;
client.UserJoined += UserJoined;
client.LogMessage += LogMessage;
client.AddService(commands);
client.AddService(new PermissionLevelService(GetPermissions));
commands.CreateGroup("", group => GenerateCommands(group));
commands.NonCommands += Chatbot.Do;
// Load the chatbots
Chatbot.Load();
// Keep the window open in case of crashes elsewhere... (hopefully)
Thread input = new Thread(InputThread);
input.Start();
// Connection, join server if there is one in config, and start music streams
try
{
client.Run(async() =>
{
await client.Connect(config["email"].ToString(), config["password"].ToString());
if (config["server"].ToString() != "")
{
await client.AcceptInvite(client.GetInvite(config["server"].ToString()).Result);
}
await Music.StartStreams();
});
}
catch (Exception e)
{
Console.WriteLine($"Error: {e.GetBaseException().Message}");
}
}