【问题标题】:how do i create a score system with Discordpy?如何使用 Discordpy 创建评分系统?
【发布时间】:2023-04-04 11:00:02
【问题描述】:

我想创建一个不和谐的评分系统(如社交积分),但它显示默认分数(30),而我写了一个“侮辱”(我应该失去 10 个积分,所以有 20 个)

import discord
import os
import json

client = discord.Client()
insulte = ["noob", "shut up", "ur mom", "ez"]

@client.event
async def on_ready():
  print('Je me suis lancé en {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return
  score = 30

  msg = message.content

  if msg.startswith('$cs hello'):
    await message.channel.send('hello!')


  if msg.startswith('$cs cs'):
    await message.channel.send('vous avez ' + str(score))


  if any(word in msg for word in insulte):
    await message.channel.send('vous avez perdu 10 crédits.')
    score = score - 10

client.run(os.getenv('TOKEN'))

有人可以帮助我吗? (是的,我是法国人。)

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    每次发送消息时,score 设置为 30。为避免这种情况,请将赋值移到 on_message 之外并将其用作全局变量。

    import discord
    import os
    import json
    
    client = discord.Client()
    insulte = ["noob", "shut up", "ur mom", "ez"]
    
    @client.event
    async def on_ready():
      print('Je me suis lancé en {0.user}'.format(client))
    
    score = 30
    
    @client.event
    async def on_message(message):
      if message.author == client.user:
        return
      global score  
    
      msg = message.content
    
      if msg.startswith('$cs hello'):
        await message.channel.send('hello!')
    
    
      if msg.startswith('$cs cs'):
        await message.channel.send('vous avez ' + str(score))
    
    
      if any(word in msg for word in insulte):
        await message.channel.send('vous avez perdu 10 crédits.')
        score = score - 10
    
    client.run(os.getenv('TOKEN'))
    

    【讨论】:

    • 谢谢,我会测试这个,但是这个线程会保持开放一段时间(如果这是合法的,idk)因为这只是评分系统的开始,之后我会尝试为每个评分不和谐用户个人(我不知道该怎么做)
    【解决方案2】:

    我建议更换

    if any(word in msg for word in insulte):
        await message.channel.send('vous avez perdu 10 crédits.')
        score = score - 10
    

    与

    for word in insulte:
         if word in msg:
            score -= 10
    

    此外,您可能还想检查是否有任何其他 if 语句被命中。 P.S 我对 Discord.py 不熟悉,但是在发送消息之前尝试从分数中扣除

    【讨论】:

    • 谢谢,我会测试这个,但现在,我有一个新问题,但它与分数无关,而是与数据库中的列表有关,所以我将创建一个新线程。 ..
    猜你喜欢
    • 2023-03-10
    • 2015-11-14
    • 2014-07-12
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多