public async Task RemoveAsync(CommandContext ctx, [Description("Channel the tag originated from")] DiscordChannel origin, [Description("Name of tag to copy"), RemainingText] string name) { using (var db = this.Database.CreateContext()) { if (db.Tags.Any(x => x.Name == name && x.ChannelId == (long)origin.Id)) { var tag = db.Tags.First(x => x.Name == name && x.ChannelId == (long)origin.Id); if (!db.Tags.Any(x => x.Name == name && x.ChannelId == (long)ctx.Channel.Id)) { var newtag = new DatabaseTag { ChannelId = (long)ctx.Channel.Id, Contents = tag.Contents, Name = tag.Name, OwnerId = (long)ctx.Member.Id, CreatedAt = DateTime.Now }; db.Tags.Add(newtag); await db.SaveChangesAsync(); await ctx.SafeRespondAsync($"Tag `{name.BreakMentions()}` successfully copied from {origin.Mention}!"); } else { await ctx.SafeRespondAsync($"Tag `{name.BreakMentions()}` already exists in this channel!"); } } else { await ctx.SafeRespondAsync($"No such tag exists!"); } } }
/// <summary> /// 获取标签的属性 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="databaseTag"></param> /// <param name="tag"></param> private static void GetDbTagInfoAndAdd <T>(PLCDevice plcDevice, DatabaseTag databaseTag, Tag <T> tag) { // tag.DataType = (TagDataType)databaseTag.DataType;//类型自动判断了 tag.CycleRead = databaseTag.CycleRead; tag.Length = databaseTag.Length; tag.TagName = databaseTag.TagName; tag.TagAddr = databaseTag.TagAddr; tag.GroupId = databaseTag.GroupId; tag.description = databaseTag.description; plcDevice.AddToGroup <T>(tag); }
public async Task SetAsync(CommandContext ctx, [Description("Tag to create")] string name, [Description("Contents of tag"), RemainingText] string contents) { using (var db = this.Database.CreateContext()) { if (db.Tags.Any(x => x.Name == name && x.ChannelId == (long)ctx.Channel.Id)) { var tag = db.Tags.First(x => x.Name == name && x.ChannelId == (long)ctx.Channel.Id); if (tag.OwnerId != (long)ctx.Member.Id) { await ctx.SafeRespondAsync("That tag already exists for this channel and you don't own it!"); } else { tag.Contents = contents; db.Tags.Update(tag); await db.SaveChangesAsync(); await ctx.SafeRespondAsync($"Succesfully modified your tag `{name.BreakMentions()}`!"); } return; } var t = new DatabaseTag { ChannelId = (long)ctx.Channel.Id, Contents = contents, Name = name, OwnerId = (long)ctx.Member.Id, CreatedAt = DateTime.Now }; db.Tags.Add(t); await db.SaveChangesAsync(); await ctx.SafeRespondAsync($"Tag `{name.BreakMentions()}` succesfully created!"); } }