LA /

Bots

Comment créer un bot?

https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token

Un petit bot pour afficher la liste des connectés en html

##        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
##                    Version 2, December 2004
##
## Copyright (C) Pork Team
##
## Everyone is permitted to copy and distribute verbatim or modified
## copies of this license document, and changing it is allowed as long
## as the name is changed.
##
##            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
##   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
##
##  0. You just DO WHAT THE FUCK YOU WANT TO.
##

import discord

token = open("token.txt", "r").read().strip()
bot = discord.Client()
channel_id = 278706666176774144 # id du salon #radio

mute_icon = """<svg name="Nova_MicrophoneMute" class="icon-3BcwQu" aria-hidden="false" width="16" height="16" viewBox="0 0 24 24"><path d="M6.7 11H5C5 12.19 5.34 13.3 5.9 14.28L7.13 13.05C6.86 12.43 6.7 11.74 6.7 11Z" fill="currentColor"></path><path d="M9.01 11.085C9.015 11.1125 9.02 11.14 9.02 11.17L15 5.18V5C15 3.34 13.66 2 12 2C10.34 2 9 3.34 9 5V11C9 11.03 9.005 11.0575 9.01 11.085Z" fill="currentColor"></path><path d="M11.7237 16.0927L10.9632 16.8531L10.2533 17.5688C10.4978 17.633 10.747 17.6839 11 17.72V22H13V17.72C16.28 17.23 19 14.41 19 11H17.3C17.3 14 14.76 16.1 12 16.1C11.9076 16.1 11.8155 16.0975 11.7237 16.0927Z" fill="currentColor"></path><path d="M21 4.27L19.73 3L3 19.73L4.27 21L8.46 16.82L9.69 15.58L11.35 13.92L14.99 10.28L21 4.27Z" fill="currentColor"></path></svg>"""
deaf_icon = """<svg name="Nova_HeadsetDeafen" class="icon-3BcwQu" aria-hidden="false" width="16" height="16" viewBox="0 0 24 24"><path d="M6.16204 15.0065C6.10859 15.0022 6.05455 15 6 15H4V12C4 7.588 7.589 4 12 4C13.4809 4 14.8691 4.40439 16.0599 5.10859L17.5102 3.65835C15.9292 2.61064 14.0346 2 12 2C6.486 2 2 6.485 2 12V19.1685L6.16204 15.0065Z" fill="currentColor"></path><path d="M19.725 9.91686C19.9043 10.5813 20 11.2796 20 12V15H18C16.896 15 16 15.896 16 17V20C16 21.104 16.896 22 18 22H20C21.105 22 22 21.104 22 20V12C22 10.7075 21.7536 9.47149 21.3053 8.33658L19.725 9.91686Z" fill="currentColor"></path><path d="M3.20101 23.6243L1.7868 22.2101L21.5858 2.41113L23 3.82535L3.20101 23.6243Z" fill="currentColor"></path></svg>"""

def member_list(channel):
    members = channel.members
    f = open("memberlist.html","w+")
    f.write("""<style type="text/css">img.avatar {width: 24px; length: 24px; border-radius: 50%;}</style>""")
    f.write("<table>")
    for m in members:
        #print("<tr><td>" + m.id + "</td>")
        f.write('<tr>')
        f.write('<td><img class="avatar" src="' + str(m.avatar_url_as(size=32)) + '" alt="' + str(m.name) + '" </td>')
        f.write("<td>" + str(m.name) + "</td>")
        if m.voice.channel.id == channel.id:
            if m.voice.self_mute or m.voice.mute:
                f.write("<td>" + mute_icon + "</td>")
            else : f.write("<td> </td>")
            if m.voice.self_deaf or m.voice.deaf:
                f.write("<td>" + deaf_icon + "</td>")
            else : f.write("<td> </td>")
        f.write("</tr>")
    f.write("</table>")
    f.close()

@bot.event
async def on_ready():
    await bot.change_presence(status=discord.Status.dnd, activity=discord.Activity(type=discord.ActivityType.watching, name="le drama"))
    channel = bot.get_channel(channel_id)
    member_list(channel)

@bot.event
async def on_voice_state_update(member, before, after):
    channel = bot.get_channel(channel_id)
    member_list(channel)

bot.run(token)

Twitter Bot

Fait à l'arrache mais ça marche

##        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
##                    Version 2, December 2004
##
## Copyright (C) Pork Team
##
## Everyone is permitted to copy and distribute verbatim or modified
## copies of this license document, and changing it is allowed as long
## as the name is changed.
##
##            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
##   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
##
##  0. You just DO WHAT THE FUCK YOU WANT TO.
##

import tweepy, discord, os
from discord.ext.commands import *
from dotenv import load_dotenv
load_dotenv()

# twitter authentication
auth = tweepy.OAuthHandler(os.getenv('CONSUMER_KEY'), os.getenv('CONSUMER_SECRET'))
auth.set_access_token(os.getenv('ACCESS_TOKEN'), os.getenv('ACCESS_TOKEN_SECRET'))
# discord authentication
discord_token = os.getenv('DISCORD_TOKEN')

twitter_role = "Twitter"

twitter = tweepy.API(auth)
bot = Bot("!")

async def send_tweet(ctx):
    msg = ctx.message.content.split(' ',1)[1]
    if len(msg) > 280 :
        await ctx.send(ctx.message.author.mention + " 280 caractères maximum")
    else:
        status = twitter.update_status(msg)
        await ctx.send(ctx.message.author.mention + " : https://twitter.com/AntenneLibre/status/" + str(status.id))

@bot.command()
@has_role(twitter_role)
async def tweet(ctx, args):
   await send_tweet(ctx)

bot.run(discord_token)