How to make a number alignment in JSON? - python

I am working on a command list command in JSON and I am having troubles with making a number alignment in the code.
#client.command()
async def command_list(ctx):
with open("commands.json", "r") as f:
y = json.load(f)
embed = discord.Embed(description="\n".join(y[str(ctx.guild.id)]),
colour=discord.Colour())
embed.set_author(name=ctx.author, icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
I want it to be sent like this:
1. <1stcommandname>
2. <2ndcommandname>
3. <3rdcommandname>
...

Related

Adding Roles In Slash Commands (Discord.Py)

#tree.command(name = 'redeem', description = 'Redeems A Members Key')
async def redeem(interaction: discord.Interaction, key: str, member:discord.Member):
with open("bkeys.txt") as f:
if key in f.read():
em = discord.Embed(color=0xff0000)
em.add_field(name="Invalid Key", value="Sorry, this key has been blacklisted")
await interaction.response.send_message(embed=em)
return 0
with open("keys.txt") as f:
if key in f.read():
role = interaction.guild.get_role(1071561081685811210)
await member.add_roles(member, role)
em = discord.Embed(color=0x008525)
em.add_field(name="Key Redeemed", value="Key has now been redeemed")
await interaction.response.send_message(embed=em)
f = open("ukeys.txt", "w")
f.write(key)
f.write('\n')
else:
em = discord.Embed(color=0xff0000)
em.add_field(name="Invalid Key", value="Inputed key has already been used!")
await interaction.response.send_message(embed=em)
Error
**This has been a command I have been trying to work on its just the add roles will not work, btw im new to python so I don't know much sorry, so if anyone could just drop the code please.
**
I tried changing the (1071561081685811210) to my role name ("Buyer") and asking for help but I didn't understand.
like bruh has said you should change
member.add_roles(member,role)
to
member.add_roles(role)

Discord Py - Appending info to an external file but program can't read the last entry (altho info gets saved)

New to programming. I'm creating a bot for moderators to view and add to a filter trigger word list, which is saved as an external text file. When the command to add a word is evoked, it gets added to the text file. But when I make the command to view the list in the server, the printed list doesn't get updated and the last entry just made is not there.
with open('triggers.txt', 'r') as x:
global triggerlist
trig = x.read()
triggerlist = trig.split()
#client.command(aliases = ["viewlist", "viewtriggers"]) #view
#has_permissions(ban_members = True)
async def viewblocklist(ctx):
channel = client.get_channel(LOG_CHANNEL_ID)
await ctx.message.add_reaction(emoji_thumbsup)
printformat = '\n'.join(triggerlist)
await channel.send(printformat)
#client.command(aliases = ["addlist", "addtrigger"]) #add/append
#has_permissions(ban_members = True)
async def addblocklist(ctx, *, arg):
with open('triggers.txt', 'a+') as x:
x.write(arg.strip().lower()+'\n')
x.flush()
x.close()
await ctx.message.add_reaction(emoji_thumbsup)
return

Discord Py Bot - json file is being written to but does not save after reload

I have a json load/save/dump function to count how many time a single word is said in a specific channel. It works great, but I lose the data after reboot of the bot. Below is my code.
def load_counters():
with open('cup.json', 'r') as f:
counters = json.load(f)
return counters
def save_counters(counters):
with open('cup.json', 'w') as f:
json.dump(counters, f)
if message.channel.id == 709551578612498453:
if message.content == ('cup'):
counters = load_counters()
counters["cup"] += 1
save_counters(counters)
return
else:
cup_meta = client.get_channel(709984510678269982)
cup_channel = client.get_channel(709551578612498453)
await cup_meta.send(message.author.mention + ' has violated the sacred rules of Cup')
await message.delete()
await cup_channel.send('cup')
return
with open('cup.json', 'r') as f:
counters1 = json.load(f) # Open and load the file
totalcup = counters1['cup']
if message.content == ('!totalcup'):
await message.channel.send(f"Cup has been said {totalcup} times since Bender reset me.")
Here is the json file - right now if I were to run !totalcup, the bot spits out '13' but the file says 0. Not sure if I am missing something as I am new to code.
{
"cup": 0
}
I just figured it out. The code does work as intended, it is a problem with how my host (Heroku) operates. There isn't anything I can do until I find a new hosting situation.

Facing issues with a discord.py "hug command"

I am currently making a Discord.py bot that responds to commands with a random image that has been specified, here is my code:
hug_gifs = ['https://c.tenor.com/nHkiUCkS04gAAAAC/anime-hug-hearts.gif']
hug_names = ['Hugs you!']
#bot.command()
async def hug(ctx):
embed = discord.Embed [
colour=(discord.Colour.red()).
description = f"{ctx.author.mention} {(random.choice(hug_names))}"
)
embed.set_image(url=(random.choice(hug_gifs)))
await ctx.send(embed = embed)
There are two mistakes when you are trying to create the discord.Embed:
When passing the arguments to the discord.Embed object, you are starting the initializer with a [, which should be changed to (. In Python the [ and ] shall be used only to create a list or to reference any of its positions.
You are using a dot . as separator for the different arguments taken by the constructor of discord.Embed, but that should be a comma , .
Here is your corrected code:
hug_gifs = ['https://c.tenor.com/nHkiUCkS04gAAAAC/anime-hug-hearts.gif']
hug_names = ['Hugs you!']
#bot.command()
async def hug(ctx):
embed = discord.Embed (
colour=(discord.Colour.red()),
description = f"{ctx.author.mention} {(random.choice(hug_names))}"
)
embed.set_image(url=(random.choice(hug_gifs)))
await ctx.send(embed = embed)

Getting emoji object to unicode

Right now im my journey of discordbot making, I'm tackling reaction roles! I think I have most everything setup, but I can't seem to find a way to get from an emoji object to an emoji unicode, so I can compare them and add the reaction. Here's what I have so far:
#bot.event
async def on_raw_reaction_add(payload):
print(payload)
channel = bot.get_channel(payload.channel_id)
emoji = payload.emoji
print(emoji.id)
#bot.command(pass_context=True)
async def reactionrole(ctx, title:str, description:str, name:str, value:str):
title = ''.join(title)
description = ''.join(description)
name = ''.join(name)
value = ''.join(value)
embed = discord.Embed(colour = discord.Colour.teal(), title = title, description = description)
embed.add_field(name = name, value = value, inline=False)
message = await ctx.send(embed = embed)
with open("reactionroles.json", 'r') as f:
data = json.load(f)
addon = {"guild-id" : ctx.guild.id}, {"message-id" : message.id}, {"roles" : []}, {"emojis" : []}
data[ctx.guild.id] = addon
with open("reactionroles.json", 'w') as f:
json.dump(data, f)
#bot.command(pass_context=True)
async def reactionadd(ctx, emoji, *role:str):
role = ''.join(role)
with open("reactionroles.json", 'r') as f:
data = json.load(f)
message_id = data[str(ctx.guild.id)][1]['message-id']
message_id = int(message_id)
message = await ctx.channel.fetch_message(message_id)
await message.add_reaction(emoji)
role = discord.utils.get(ctx.guild.roles, name=role)
data[str(ctx.guild.id)][3]['emojis'].append(emoji)
print(role)
data[str(ctx.guild.id)][2]['roles'].append(str(role))
with open("reactionroles.json", 'w') as f:
json.dump(data, f, indent=4)
So the command reactionrole creates the embed, and writes some placeholder data into a json file. Afterwards, the reactionadd command adds the emoji unicode to a json array, and the role to a different json array. When on a reaction add, in on_raw_reaction_add(payload), payload only has the name of the emoji, and not the unicode. Because of this, I', not able to compare the two to see what role goes to what emoji. I can't save the origional, in reactionadd because I would run into problems in on_reaction_add. I'm lost on getting the unicode from the payload this is my final reach out. Here's what's inside payload:
<RawReactionActionEvent message_id=759903170721087508 user_id=146348630926819328 channel_id=754904403710050375 guild_id=665787149513261057 emoji=<PartialEmoji animated=False name='�😋' id=Non
event_type='REACTION_ADD' member=<Member id=146348630926819328 name='Chai' discriminator='6396' bot=False nick=None guild=<Guild id=665787149513261057 name="ChaiBot's Playground" shard_id=None chunked=True member_count=34>>>
From an Emoji object you can get the unicode character from emoji.name, or if you are looking for the unicode name you can use the python unicodedata library:
import unicodedata
unicodedata.name(emoji.name)
I have realized my error! I was able to compare the unicode from reactionadd to payload.emoji the entire time! I was not able to see originally though, that vs code shows the unicode as an emoji (in terminal), and not as the string. That makes much more sense now! Thanks to all they helped!

Categories

Resources