Home > Mobile >  how do i create a score system with Discordpy?
how do i create a score system with Discordpy?

Time:11-28

I want to create a score system with discordpy (like socials credits), but it shows the default score (30) while i writed a "insulte" (I am supposed to have lost 10 credits, so have 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'))

can someone help me ? (yes i'm french.)

CodePudding user response:

Every time a message is sent, score is set to 30. To avoid this, move the assignment outside on_message and use it as a global variable.

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'))

CodePudding user response:

I would recommend replacing

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

with

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

Also, you might want to check if any of the other if statements are being hit. P.S I'm not familiar with Discord.py but try deducting from the score before sending the message

  • Related