public async Task RemoveLoudRoles(Discord.WebSocket.SocketUser Author) { // If the user is in any mentionable rank roles, they will be removed. var Us = (Discord.WebSocket.SocketGuildUser)Author; // For each loud role, which can be a loud big role or a loud tiny role: foreach (var LoudRole in Us.Roles.Where(x => Ranking.LoudDigitRoles.Contains(x.Name) || Ranking.LoudMetalRoles.Contains(x.Name))) { await Us.RemoveRoleAsync(LoudRole); } }
/// <summary> /// Broadcast changed user to all his connected instances /// </summary> /// <param name="user">Discord user</param> /// <param name="eventName">Event name</param> /// <returns>Completed task</returns> public static Task BroadcastUserChange(DW.SocketUser user, string eventName) { foreach (KeyValuePair <WebSocket, DW.SocketUser> socket in CommandHandler.sockets) { if (socket.Value.Id == user.Id) { socket.Key.Send(eventName, new Wrappers.User(user)); } } return(Task.CompletedTask); }
/// <summary> /// Register client /// </summary> /// <param name="socket">Web socket</param> /// <param name="userId">User to register</param> private static void RegisterClient(WebSocket socket, ulong userId) { if (userId > 0) { DW.SocketUser user = Program.DiscordClient.GetUser(userId); int mutual = user.MutualGuilds.Count; if (user != null && mutual > 0) { CommandHandler.ClientAdd(socket, user); socket.Send("auth_done", new Wrappers.User(user)); } else if (mutual == 0) { socket.Send("auth_done", null); } } }
async Task Client_UserVoiceStateUpdated(Discord.WebSocket.SocketUser arg1, Discord.WebSocket.SocketVoiceState arg2, Discord.WebSocket.SocketVoiceState arg3) { if (arg1.IsBot) { return; } if (TTTGame.IsTTTVoice(arg2.VoiceChannel) || TTTGame.IsTTTVoice(arg3.VoiceChannel)) { return; } var thread = new Thread(handle); thread.Start(new passing() { arg1 = arg1, arg2 = arg2, arg3 = arg3 }); }
/// <summary> /// Run roles-list command /// </summary> /// <param name="socket">Web socket</param> /// <param name="user">Discord user who invoked command</param> /// <param name="parameters">Command parameters</param> /// <returns>True on success</returns> public bool Run(WebSocket socket, DW.SocketUser user, string[] parameters) { if (parameters.Length == 0 || string.IsNullOrWhiteSpace(parameters[0])) { // Not enough parameters return(false); } ulong guildId = 0; if (!ulong.TryParse(parameters[0], out guildId)) { // NO guild specified return(false); } DW.SocketGuild guild = user.MutualGuilds.FirstOrDefault(mutualGuild => mutualGuild.Id == guildId); if (guild == null) { // User is not in this guild return(false); } ulong userId = 0; if (parameters.Length > 1 && !string.IsNullOrWhiteSpace(parameters[1]) && ulong.TryParse(parameters[1], out userId)) { // List roles for specific user socket.Send("rolesList", guild.GetUser(userId).Roles.Select(role => new Wrappers.Role(role))); return(true); } // List all roles in guild socket.Send("rolesList", guild.Roles.Select(role => new Wrappers.Role(role))); return(true); }
/// <summary> /// Run role-remove command /// </summary> /// <param name="socket">Web socket</param> /// <param name="user">Discord user who invoked command</param> /// <param name="parameters">Command parameters</param> /// <returns>True on success</returns> public bool Run(WebSocket socket, DW.SocketUser user, string[] parameters) { ulong guildId = 0; ulong roleId = 0; if (parameters.Length > 1 || string.IsNullOrWhiteSpace(parameters[0]) || string.IsNullOrWhiteSpace(parameters[1]) || ulong.TryParse(parameters[0], out guildId) || ulong.TryParse(parameters[1], out roleId) || guildId == 0 || roleId == 0) { // Not enough parameters return(false); } DW.SocketGuild foundGuild = user.MutualGuilds.FirstOrDefault(guild => guild.Id == guildId); if (foundGuild != null) { DW.SocketGuildUser foundUser = foundGuild.GetUser(user.Id); if (foundUser != null && foundUser.Roles.Any(userRole => userRole.Permissions.Administrator)) { DW.SocketRole role = foundGuild.GetRole(roleId); if (role != null) { Task.Run(() => role.DeleteAsync()); return(true); } } } return(false); }
public async Task AddSpectralRoles(Discord.WebSocket.SocketUser Author, Rank rank) { var Us = (Discord.WebSocket.SocketGuildUser)Author; // First add the non-digit role. string nonDigitSpectralName = rank.SpectralMetalPrint(); var spectralRole = this._socket.Roles.FirstOrDefault(x => x.Name == nonDigitSpectralName); if (spectralRole != null) { await Us.AddRoleAsync(spectralRole); } // Then, if the rank has a digit role, add that too. if (rank.Digits()) { string digitSpectralName = rank.SpectralFullPrint(); var digitSpectralRole = _socket.Roles.FirstOrDefault(x => x.Name == digitSpectralName); if (digitSpectralRole != null) { await Us.AddRoleAsync(digitSpectralRole); } } }
public async Task AddLoudRoles(Discord.WebSocket.SocketUser Author, Rank rank) { var Us = (Discord.WebSocket.SocketGuildUser)Author; // First add the non-digit role. string nonDigitLoudName = rank.CompactMetalPrint(); var LoudRole = _socket.Roles.FirstOrDefault(x => x.Name == nonDigitLoudName); if (LoudRole != null) { await Us.AddRoleAsync(LoudRole); } // Then, if the rank has a digit role, add that too. if (rank.Digits()) { string digitLoudName = rank.CompactFullPrint(); var LoudDigitRole = _socket.Roles.FirstOrDefault(x => x.Name == digitLoudName); if (LoudDigitRole != null) { await Us.AddRoleAsync(LoudDigitRole); } } }
/// <summary> /// Converts an existing <see cref="SocketUser"/> to an abstracted <see cref="ISocketUser"/> value. /// </summary> /// <param name="socketUser">The existing <see cref="SocketUser"/> to be abstracted.</param> /// <exception cref="ArgumentNullException">Throws for <paramref name="socketUser"/>.</exception> /// <returns>An <see cref="ISocketUser"/> that abstracts <paramref name="socketUser"/>.</returns> public static ISocketUser Abstract(this SocketUser socketUser) => socketUser switch { null => throw new ArgumentNullException(nameof(socketUser)),
public static async Task <bool> SendPartnerResponse(DiscordSocketClient client, Storage.UserData partnerData, Discord.WebSocket.SocketUser user, bool bThemeOnly = false) { if (bThemeOnly) { var message = (string.IsNullOrWhiteSpace(Storage.xs.Entries.GetTheme()) ? "none" : string.Format(Properties.Resources.TRADE_THIS_THEME, Storage.xs.Entries.GetTheme())) + "\n"; await user.SendMessageAsync(embed : Utils.EmbedMessage(client, message, Utils.Emotion.positive)); } else { if (string.IsNullOrWhiteSpace(partnerData.ReferenceDescription) && string.IsNullOrWhiteSpace(partnerData.ReferenceUrl)) { return(false); } string message = string.Format(Properties.Resources.REF_TRADE_PARTNER, user.Id, $"{partnerData.UserName}" + (string.IsNullOrWhiteSpace(partnerData.NickName) ? "" : $" ({partnerData.NickName})")); if (!string.IsNullOrWhiteSpace(Storage.xs.Entries.GetTheme())) { message += $"\n{string.Format(Properties.Resources.TRADE_THIS_THEME, Storage.xs.Entries.GetTheme())}"; } if (Storage.xs.Settings.GetTradeDays() != 0) { message += $"\n{string.Format(Properties.Resources.TRADE_ENDS_ON, Storage.xs.Settings.GetTradeDays(), Storage.xs.Settings.GetTradeStart(Storage.xs.Settings.GetTradeDays()).ToString("dd-MMMM"))}"; } if (!string.IsNullOrWhiteSpace(partnerData.ReferenceDescription)) { message += $"\n`description` : *\"{partnerData.ReferenceDescription}\"*"; } await user.SendMessageAsync(embed : Utils.EmbedMessage(client, message, Utils.Emotion.positive, partnerData.ReferenceUrl)); } return(true); }
/// <summary> /// Constructs a new <see cref="SocketUserAbstraction"/> around an existing <see cref="WebSocket.SocketUser"/>. /// </summary> /// <param name="socketUser">The value to use for <see cref="WebSocket.SocketUser"/>.</param> /// <exception cref="ArgumentNullException">Throws for <paramref name="socketUser"/>.</exception> public SocketUserAbstraction(SocketUser socketUser) { SocketUser = socketUser ?? throw new ArgumentNullException(nameof(socketUser)); }
/// <inheritdoc /> public string GetDefaultAvatarUrl() => SocketUser.GetDefaultAvatarUrl();
internal new static SocketInteraction Create(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) { var entity = new SocketCommandBase(client, model, channel, user); entity.Update(model); return(entity); }
internal new static SocketUserMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model) { var entity = new SocketUserMessage(discord, model.Id, channel, author, MessageHelper.GetSource(model)); entity.Update(state, model); return(entity); }
internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author, MessageSource source) : base(discord, id, channel, author, source) { }
/// <inheritdoc cref="SocketUser.ToString" /> public override string ToString() => SocketUser.ToString();
/// <inheritdoc /> public async Task <IDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null) => (await SocketUser.GetOrCreateDMChannelAsync(options)) .Abstract();
/// <summary> /// Add client to message loop /// </summary> /// <param name="socket">Connected client</param> /// <param name="user">Discord user</param> public static void ClientAdd(WebSocket socket, DW.SocketUser user) { sockets.TryAdd(socket, user); }
internal static SocketMessage Create(DiscordSocketClient discord, ClientState state, SocketUser author, ISocketMessageChannel channel, Model model) { if (model.Type == MessageType.Default) { return(SocketUserMessage.Create(discord, state, author, channel, model)); } else { return(SocketSystemMessage.Create(discord, state, author, channel, model)); } }
internal override void Update(ClientState state, Model model) { base.Update(state, model); SocketGuild guild = (Channel as SocketGuildChannel)?.Guild; if (model.IsTextToSpeech.IsSpecified) { _isTTS = model.IsTextToSpeech.Value; } if (model.Pinned.IsSpecified) { _isPinned = model.Pinned.Value; } if (model.EditedTimestamp.IsSpecified) { _editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks; } if (model.MentionEveryone.IsSpecified) { _isMentioningEveryone = model.MentionEveryone.Value; } if (model.RoleMentions.IsSpecified) { _roleMentions = model.RoleMentions.Value.Select(x => guild.GetRole(x)).ToImmutableArray(); } if (model.Attachments.IsSpecified) { var value = model.Attachments.Value; if (value.Length > 0) { var attachments = ImmutableArray.CreateBuilder <Attachment>(value.Length); for (int i = 0; i < value.Length; i++) { attachments.Add(Attachment.Create(value[i])); } _attachments = attachments.ToImmutable(); } else { _attachments = ImmutableArray.Create <Attachment>(); } } if (model.Embeds.IsSpecified) { var value = model.Embeds.Value; if (value.Length > 0) { var embeds = ImmutableArray.CreateBuilder <Embed>(value.Length); for (int i = 0; i < value.Length; i++) { embeds.Add(value[i].ToEntity()); } _embeds = embeds.ToImmutable(); } else { _embeds = ImmutableArray.Create <Embed>(); } } if (model.UserMentions.IsSpecified) { var value = model.UserMentions.Value; if (value.Length > 0) { var newMentions = ImmutableArray.CreateBuilder <SocketUser>(value.Length); for (int i = 0; i < value.Length; i++) { var val = value[i]; if (val.Object != null) { var user = Channel.GetUserAsync(val.Object.Id, CacheMode.CacheOnly).GetAwaiter().GetResult() as SocketUser; if (user != null) { newMentions.Add(user); } else { newMentions.Add(SocketUnknownUser.Create(Discord, state, val.Object)); } } } _userMentions = newMentions.ToImmutable(); } } if (model.Content.IsSpecified) { var text = model.Content.Value; _tags = MessageHelper.ParseTags(text, Channel, guild, _userMentions); model.Content = text; } if (model.ReferencedMessage.IsSpecified && model.ReferencedMessage.Value != null) { var refMsg = model.ReferencedMessage.Value; ulong? webhookId = refMsg.WebhookId.ToNullable(); SocketUser refMsgAuthor = null; if (refMsg.Author.IsSpecified) { if (guild != null) { if (webhookId != null) { refMsgAuthor = SocketWebhookUser.Create(guild, state, refMsg.Author.Value, webhookId.Value); } else { refMsgAuthor = guild.GetUser(refMsg.Author.Value.Id); } } else { refMsgAuthor = (Channel as SocketChannel).GetUser(refMsg.Author.Value.Id); } if (refMsgAuthor == null) { refMsgAuthor = SocketUnknownUser.Create(Discord, state, refMsg.Author.Value); } } else { // Message author wasn't specified in the payload, so create a completely anonymous unknown user refMsgAuthor = new SocketUnknownUser(Discord, id: 0); } _referencedMessage = SocketUserMessage.Create(Discord, state, refMsgAuthor, Channel, refMsg); } if (model.Stickers.IsSpecified) { var value = model.Stickers.Value; if (value.Length > 0) { var stickers = ImmutableArray.CreateBuilder <Sticker>(value.Length); for (int i = 0; i < value.Length; i++) { stickers.Add(Sticker.Create(value[i])); } _stickers = stickers.ToImmutable(); } else { _stickers = ImmutableArray.Create <Sticker>(); } } }
/// <inheritdoc /> public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) => SocketUser.GetAvatarUrl(format, size);
internal SocketMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author) : base(discord, id) { Channel = channel; Author = author; }
internal SocketCommandBase(DiscordSocketClient client, Model model, ISocketMessageChannel channel, SocketUser user) : base(client, model.Id, channel, user) { var dataModel = model.Data.IsSpecified ? (DataModel)model.Data.Value : null; ulong?guildId = null; if (Channel is SocketGuildChannel guildChannel) { guildId = guildChannel.Guild.Id; } Data = SocketCommandBaseData.Create(client, dataModel, model.Id, guildId); }