【问题标题】:How to add emoji to a string then add to .ToList如何将表情符号添加到字符串然后添加到 .ToList
【发布时间】:2019-12-23 10:22:39
【问题描述】:

我目前正在尝试为我的 Discord BOT 创建一个投票命令。我想编辑字符串answer 并根据给出的答案的数字在该答案的开头添加一个表情符号。

EG 用户将输入 ?poll "questionhere" "answer1" "answer2" - 我想选择 "answer1" 并更改为 ":one: answer1" 等等,但我不知道该怎么做这样做。

如果可能的话,我想建议的第二件事是我目前如何管理有多少反应被添加到消息中,必须有一种更好的方法来做我正在做的事情,但我仍然很陌生这和学习所以任何关于如何更好地构建以下内容的建议或建议将不胜感激。

//Take the string answer and split into separate strings based on text inside ""
var result = answer.Split('"').Where((x, i) => i % 2 == 1).ToList();

if (result.Count() == 2)
 {

 List<List<string>> listChunks = StaticObjects.SplitIntoChunks<string>(result, 50);

 for (int i = 0; i < listChunks.Count; i++)
  {
    listChunks = listChunks.OrderBy(x => x).ToList();
    string description = String.Join(Environment.NewLine, listChunks[i]);

  EmbedBuilder eb = new EmbedBuilder()
  {
    Title = $@":bar_chart:**{question.ToUpper()}**",
    Description = description,
    Color = new Color(0, 0, 127),
  };

var message = await ReplyAsync(embed: eb.Build());
var YourEmoji1 = new Emoji("1\u20e3");
var YourEmoji2 = new Emoji("2\u20e3");
await message.AddReactionAsync(YourEmoji1);
await message.AddReactionAsync(YourEmoji2);

目前继续if (result.Count() == 3)

更新 我找到了一种在字符串前添加表情符号的方法,但是现在我不知道如何将其传递回原始 result

var one = "1\u20e3" + result.ElementAt(0);

更新 2 我现在已经设法让它工作了:)

添加了这个

if (result.Count() == 2)
  {
  var one = "1\u20e3" + " " + "-" + " " + result.ElementAt(0); 
  var two = "2\u20e3" + " " + "-" + " " + result.ElementAt(1);

并将result 更改为one, two

string description = String.Join(Environment.NewLine, one, two);

更新 3 我发现你可以改变

var YourEmoji1 = new Emoji("1\u20e3");
var YourEmoji2 = new Emoji("2\u20e3");
await message.AddReactionAsync(YourEmoji1);
await message.AddReactionAsync(YourEmoji2);

await message.AddReactionAsync(new Emoji("1\u20e3"));
await message.AddReactionAsync(new Emoji("2\u20e3"));

我现在只剩下以更优雅的方式构建它的问题,所以代码不会太长,因为我想允许最多 10 个答案

【问题讨论】:

  • 另外,AddRectionAsync() 允许你给它一个数组。所以你也可以await message.AddRectionAsync(new Emoji[] { new Emoji("1\u20e3"), new Emoji("2\u20e3")});
  • 谢谢,我试一试,这还会一直以正确的顺序添加表情符号吗?
  • 抱歉,响应时间长,我已尝试实施您的建议,但收到错误消息“cannot convert from 'Discord.Emoji[]' to 'Discord.IEmote'”

标签: c# discord discord.net


【解决方案1】:

既然你已经找到了你的解决方案,现在只想清理代码,我只是有一个可读性建议:

声明表情符号时无需使用字符代码 (new Emoji("1\u20e3"))。相反,您可以使用 Discord 表情符号名称,只要包含 :s。 例如:new Emoji(":one:")new Emoji(":two:")

同样可以清理以下内容

  var one = "1\u20e3" + " " + "-" + " " + result.ElementAt(0); 

到这里:

  var one = ":one: - " + result.ElementAt(0); 

等等。

这不会使您的代码工作得更好,但它使其更具可读性。

【讨论】:

    猜你喜欢
    • 2020-04-15
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多