如何在discord.py中为我的类创建实例?


import discord
from discord.ext import commands  
intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True, presences=True,guild_messages = True)

client = commands.Bot(command_prefix="!dc ", intents=intents)
#I've created the below class so that every user can have their own number of saying swear words.
class handling_swearwords :
def __init__(self,swearwords = 0):
self.swearwords = swearwords

def __del__(self):
print("One user has left the server .")

@client.event  
async def on_ready():  
print("I am ready!")
@client.event
async def on_member_join(member):   
channel = discord.utils.get(member.guild.text_channels, name="welcome")
await channel.send(f"{member.name},welcome to the server,mate.")

member_instance = member.name
member_instance = handling_swearwords()  #Creating instance to the class.

async def on_member_remove(member):
channel = discord.utils.get(member.guild.text_channels, name="Lovely-people")
await channel.send(f"{member.name},see you later mate :(")
member_instance2 = member.name
del member_instance2 #If someone leaves the server,their instance will be deleted(at least this is my thought)

@client.event
async def on_message(message):  
if message.author == client.user:  
await client.process_commands(message)
else:
with open("Badwords.txt", "r", encoding="utf-8") as f:
word = f.read()
badwords = word.split(",")
msg = message.content
for x in badwords:
if x in msg:
await message.delete()
await message.channel.send("Please do not use that word ever")

message.author.name.swearwords += 1 #Whenever user use a swearword(s),their number of swearwords will be increased.  
await client.process_commands(message) 

#I created the below function to check whether there is a attribute which is called 'swearwords' or not.

client.run(Token)

每当有人写脏话到聊天框我得到'str'对象没有属性'swearwords'错误。我不是很擅长面向对象编程,所以我无法解决这个问题。有人能帮我解决这个问题吗?谢谢你

PS:我想我得到这个错误是因为message.author.name.swearwords += 1代码行(或非idk)

听起来你在寻找一种方法来跟踪每个成员使用了多少脏话。虽然您可以创建自己的类和对象来实现这一点,但这与使用Python字典相同,只是需要额外的步骤。

我建议使用成员id而不是名称,因为id更可靠。您也不需要从每个消息的文件中读取;您可以让程序在bot启动时读取它一次。

这里有一个例子,你可以做到这一切:

import discord
from discord.ext import commands

intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True, presences=True,guild_messages = True)
client = commands.Bot(command_prefix="!dc ", intents=intents)
swearword_count = dict()  # The keys are member IDs (ints), and each value is the number of swearwords that member used (also ints).

with open("Badwords.txt", "r", encoding="utf-8") as f:
word = f.read()
badwords = word.split(",")

@client.event  
async def on_ready():  
print("I am ready!")

@client.event
async def on_member_join(member):   
channel = discord.utils.get(member.guild.text_channels, name="welcome")
await channel.send(f"{member.name},welcome to the server,mate.")
global swearword_count
swearword_count[member.id] = 0

async def on_member_remove(member):
channel = discord.utils.get(member.guild.text_channels, name="Lovely-people")
await channel.send(f"{member.name},see you later mate :(")
global swearword_count
del swearword_count[member.id]

@client.event
async def on_message(message):  
if message.author.bot:
return

global swearword_count
global badwords
msg = message.content
for x in badwords:
if x in msg:
await message.delete()
await message.channel.send("Please do not use that word ever")
swearword_count[message.author.id] += 1 #Whenever user use a swearword(s),their number of swearwords will be increased.  
return
await client.process_commands(message)

client.run(Token)

注。-message.author.name.swearwords给你错误'str' object has no attribute 'swearwords'的原因是因为message.author.name是内置Python类型str,它只有自己有限数量的内置方法,你不能添加。错误回溯应该确认错误来自哪行代码。

最新更新