अस्वीकरण: इनमें से अधिकांश का कोई मतलब नहीं हो सकता है, क्योंकि मूल रूप से यह सब सिर्फ मैं कुछ ऐसा करने की कोशिश कर रहा हूं जो मैंने पहले कभी नहीं किया है।
मैं एक "लीडरबोर्ड" प्रकार प्रणाली है कि एक कलह बॉट बनाने की कोशिश कर रहा हूँ। लेकिन मैंने जो कोशिश की, उसने मुझे एक त्रुटि दी।
यहाँ मेरा कोड है।
import discord
from discord.ext import commands
import json
from json import loads
import os
TOKEN = 'Token'
bot = commands.Bot(command_prefix='!')
@bot.event
async def on_ready():
print(f'Logged in as: {bot.user.name}')
print(f'With ID: {bot.user.id}')
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.channel.id == 782989981752098886:
if message.author == bot.user:
return
else:
if ''.join(message.content.split()).lower() == "egg":
os.chdir(r'Directory')
with open('EggData.json', 'r') as file:
data = loads(file.read())
try:
author = message.author.mention
usernameList = data
authorsAmount = usernameList[author]
print(authorsAmount)
usernameList[author] += 1
print(usernameList[author])
print(usernameList)
with open('EggData.json', 'w') as outfile:
json.dump(usernameList, outfile)
return
except:
addUsersID = {f"{author}" : 0}
appendUserID = usernameList.append(addUsersID)
with open('EggData.json', 'w') as outfile:
json.dump(appendUserID, outfile)
author = message.author.mention
usernameList = data
authorsAmount = usernameList[author]
print(authorsAmount)
usernameList[author] += 1
print(usernameList[author])
print(usernameList)
with open('EggData.json', 'w') as outfile:
json.dump(usernameList, outfile)
return
return
else:
await message.channel.send(
"{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this server. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(message.author.mention, message.author.mention))
else:
return
bot.run(TOKEN)
यह ठीक काम करता है अगर मैं मैन्युअल रूप से उपयोगकर्ता आईडी जोड़ता हूं और try:
और except:
को हटा देता हूं। लेकिन उस हिस्से के साथ जिसमें try:
और except:
हैं, वह हिस्सा है जिसे मैंने उपयोगकर्ता को सूची में जोड़ने के लिए करने की कोशिश की है यदि वे पहले से ही इस पर नहीं हैं। यहाँ जेसन कैसा दिखता है: {"<@494664629570764810>": 11, "variblePlaceholder": 4, "numbernumbernumberPlaceholder": 26}
मैं आपके किसी भी प्रश्न का उत्तर देने का प्रयास करूंगा, क्योंकि मुझे यकीन है कि यह बहुत स्पष्ट नहीं है।
संपादित करें:
गलती बताना भूल गए। मुझे जो त्रुटि मिल रही है वह है json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ट्रेसबैक त्रुटि:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "c:\Users\name\Desktop\Python\ProofOfConcept\JsonEgg\bot.py", line 27, in on_message
data = loads(file.read())
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
त्रुटि के लिए इमगुर लिंक: https://imgur.com/a/MQ0dtVc
समाधान:
मैंने दो तानाशाही को जोड़ने के लिए dict.update(dict2)
का इस्तेमाल किया।
यहाँ मेरा कोड है।
@bot.event
async def on_message(message):
await bot.process_commands(message)
if message.channel.id == 782989981752098886:
if message.author == bot.user:
return
else:
if ''.join(message.content.split()).lower() == "egg":
os.chdir(r'C:\Users\name\Desktop\Python\ProofOfConcept\JsonEgg')
with open('EggData.json', 'r') as file:
data = loads(file.read())
try:
author = message.author.mention
usernameList = data
authorsAmount = usernameList[author]
print(authorsAmount)
usernameList[author] += 1
print(usernameList[author])
print(usernameList)
with open('EggData.json', 'w') as outfile:
json.dump(usernameList, outfile)
return
except:
author = message.author.mention
usernameList = data
addUsersID = {f"{author}" : 1}
whatToAdd = usernameList
whatToAdd.update(addUsersID)
with open('EggData.json', 'w') as outfile:
json.dump(whatToAdd, outfile)
return
else:
await message.channel.send("{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this channel. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(message.author.mention, message.author.mention))
else:
return
Imgur मेरे कोड का लिंक: https://imgur.com/a/1PgF6BQ
1 उत्तर
ठीक है, यहाँ थोड़ी सी त्रुटि है। शब्दकोशों (AKA json) का उपयोग करते समय, आप बस .append() का उपयोग नहीं कर सकते क्योंकि शब्दकोश उस तरह से काम नहीं करते हैं। डिक्शनरी (json) एक की-वैल्यू पेयरिंग सिस्टम को फॉलो करते हैं, इसलिए आपको एक की और वैल्यू असाइन करने की जरूरत है जैसा कि नीचे दिखाया गया है...
उचित उपयोग है:
my_dict["myvalue"]="another value"
#So in your case:
usernameList[addUsersID]=#do something here...
आपको मिली त्रुटि शायद इसलिए है क्योंकि जेसन में कुछ भी नहीं था/प्रारूप गड़बड़ हो गया है। इसके अलावा, मैंने देखा कि आप एक कलह बॉट बना रहे हैं। Discord.py सर्वर से जुड़ने पर विचार करें, आप वहां और सहायता प्राप्त कर सकते हैं। मैंने इसे कुछ समय पहले उपयोगी पाया जब मैंने पहली बार बॉट बनाना शुरू किया। आशा है कि उपरोक्त मदद करता है!
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।